当前位置: 首页>>代码示例>>Python>>正文


Python Slider.valfmt方法代码示例

本文整理汇总了Python中matplotlib.widgets.Slider.valfmt方法的典型用法代码示例。如果您正苦于以下问题:Python Slider.valfmt方法的具体用法?Python Slider.valfmt怎么用?Python Slider.valfmt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在matplotlib.widgets.Slider的用法示例。


在下文中一共展示了Slider.valfmt方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: slide_phantom

# 需要导入模块: from matplotlib.widgets import Slider [as 别名]
# 或者: from matplotlib.widgets.Slider import valfmt [as 别名]
def slide_phantom(shot, camera='phantom2', sub=20, blur=3, interval=50, 
                  pixel_t_hist=None):
    """
    Slide through Phantom frames while displaying last closed flux surface and 
    relevant timeseries plots.
    Parameters
        shot: int, shot number
        camera: string e.g. 'phantom' (outboard midplane GPI),
                'phantom2' (X-point GPI)
        sub: number of frames to use in average subtraction. 20 works well.
        blur: extent of Gaussian blur, 1, 2, or 3 advisable
        interval: delay in ms between frames during play
        pixel_t_hist: (x,y) to show time history of that pixel instead of 
        H-alpha.
    """
    time = acquire.gpi_series(shot, camera, 'time')
    frames = acquire.video(shot, camera)
    frame_count = frames.shape[0]
    
    # Plot GPI and LCFS 
    gs = gridspec.GridSpec(3, 1, height_ratios=[3, 1, 1])
    fig, ax = plt.subplots()
    plt.subplots_adjust(bottom=0.25, hspace=.43, top=.96)
    plt.subplot(gs[0])
    im = plt.imshow(frames[0], origin='lower', cmap=plt.cm.gist_heat)
    plt.colorbar()
    plt.axis('off')

    # Plot H_alpha or pixel timeseries
    plt.subplot(gs[1]).locator_params(axis='y', nbins=4)
    if pixel_t_hist:
        plt.title('Pixel time history')
        plt.plot(time, 
                 frames[:, pixel_t_hist[0], pixel_t_hist[1]].swapaxes(0, 1))
    else: 
        plt.title('H-alpha')
        time_ha2, ha2 = process.time_crop(acquire.time_ha2(shot), time)
        plt.plot(time_ha2, ha2)
    vl1 = plt.axvline(time[0], color='r')
    plt.xlim([time[0], time[-1]])

    # Plot line average density 
    time_dens, dens = process.time_crop(acquire.time_dens(shot), time) 
    plt.subplot(gs[2]).locator_params(axis='y', nbins=4)
    plt.title('Line Average Density')
    plt.plot(time_dens, dens)
    vl2 = plt.axvline(time[0], color='r')
    plt.xlabel('Time (s)')
    plt.xlim([time[0], time[-1]])

    curr_frame = 0

    def update(val):
        global frames, curr_frame
        val = int(val)
        curr_frame = val 
        curr_time = time[val]
        slider.valtext.set_text('%d (t=%.5f s)' % (val, curr_time))
        if val < frame_count: 
            im.set_array(frames[val])
            vl1.set_xdata(curr_time)
            vl2.set_xdata(curr_time)
        fig.canvas.draw_idle()
 
    # Slider settings
    slide_area = plt.axes([0.10, 0.1, 0.65, 0.03])
    slider = Slider(slide_area, 'Frame', 0, frame_count-1, valinit=0)
    slider.drawon = True
    slider.valfmt = '%d'
    slider.on_changed(update)

    def init():
        global curr_frame
        curr_frame = int(curr_frame)
        start_frame = curr_frame
        im.set_data(frames[curr_frame])
        vl1.set_xdata(time[curr_frame])
        vl2.set_xdata(time[curr_frame])
        for i in xrange(curr_frame, curr_frame + 200, 1): 
            slider.set_val(i)
            vl1.set_xdata(time[i])
            vl2.set_xdata(time[i])
        slider.set_val(start_frame)
        return [im, vl1, vl2]

    def animate(i):
        im.set_array(frames[i])
        vl1.set_xdata(time[i])
        vl2.set_xdata(time[i])
        return [im, vl1, vl2]
    
    def play(event):
        anim = animation.FuncAnimation(fig, animate, init_func=init,
                                       frames=100, interval=interval, 
                                       blit=False)

    def forward(event):
        global curr_frame
        slider.set_val(curr_frame + 1)

#.........这里部分代码省略.........
开发者ID:sballin,项目名称:phantom_viewer,代码行数:103,代码来源:view.py

示例2: slide_corr

# 需要导入模块: from matplotlib.widgets import Slider [as 别名]
# 或者: from matplotlib.widgets.Slider import valfmt [as 别名]
def slide_corr(frames, pixel, other_pixels=None):
    """
    Display correlation values for a pixel with a slider for lag time.
    Parameters
        frames: NumPy array of correlations with dimension (no. lags, x pixels,
                y pixels)
        pixel: (x, y) to mark with a circle
        other_pixels: array of (x, y) values of pixels to mark
    """
    frame_count = frames.shape[0]

    # Initial plotting
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    plt.subplots_adjust(bottom=0.25)
    im = plt.imshow(frames[frame_count/2], origin='bottom', 
                    cmap=plt.cm.RdBu_r, vmin=-1, vmax=1)
    circ = plt.Circle(pixel[::-1], radius=1, edgecolor='r', fill=False)
    ax.add_patch(circ)
    if other_pixels: 
        t_hists_r = [o[0] for o in other_pixels]
        t_hists_z = [o[1] for o in other_pixels]
    else:
        t_hists_r = acquire.gpi_series(1150611004, 'phantom2', 'hist_xpix')
        t_hists_z = acquire.gpi_series(1150611004, 'phantom2', 'hist_ypix')
    for pos in zip(t_hists_r, t_hists_z): 
        ax.add_patch(plt.Circle(pos, radius=1, edgecolor='b', fill=False))
    plt.colorbar()
    plt.title('Correlation for pixel (%d, %d)' % pixel)
    plt.xlabel('Pixel y coordinate')
    plt.ylabel('Pixel x coordinate')

    # Slider and button settings
    slide_area = plt.axes([0.10, 0.1, 0.65, 0.03])
    slider = Slider(slide_area, 'Lag', -frame_count/2, frame_count/2, valinit=0)
    slider.drawon = True
    slider.valfmt = '%d'
    play_button_area = plt.axes([0.45, 0.025, 0.1, 0.04])
    play_button = Button(play_button_area, 'Play')

    curr_frame = 0

    def update(val):
        global curr_frame
        curr_frame = val + frame_count/2
        slider.valtext.set_text('%d' % val)
        if curr_frame < frame_count: 
            im.set_array(frames[curr_frame])
        fig.canvas.draw_idle()

    slider.on_changed(update)

    def init():
        global curr_frame
        curr_frame = int(curr_frame)
        im.set_data(frames[curr_frame])
        for i in xrange(curr_frame, curr_frame + 200, 2): 
            slider.set_val(i)
        return [im]

    def animate(i):
        im.set_array(frames[i])
        return [im]
    
    def play(event):
        anim = animation.FuncAnimation(fig, animate, init_func=init, frames=
                                       frame_count, interval=0, blit=False)

    play_button.on_clicked(play)
    plt.show()
开发者ID:sballin,项目名称:phantom_viewer,代码行数:72,代码来源:view.py

示例3: slide_reconstruction

# 需要导入模块: from matplotlib.widgets import Slider [as 别名]
# 或者: from matplotlib.widgets.Slider import valfmt [as 别名]
def slide_reconstruction(shot, smoothing_param=100, save=False):
    """
    Slide through Phantom camera frames and their reconstructions from
    synthetic field line images calculated externally with Julia.
    
    Args:
        shot: [int] shot number
    """
    # Read cache files broken up by EFIT time segment
    old_working_dir = os.getcwd()
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
    emissivity_files = sorted(glob.glob('../cache/fl_emissivities_Xpt_{}_sp{}_*'.format(shot, smoothing_param)))
    fl_data_files = sorted(glob.glob('../cache/fl_data_Xpt_{}_*'.format(shot)))
    fl_image_files = sorted(glob.glob('../cache/fl_images_Xpt_{}_*'.format(shot)))
    if len(emissivity_files) == 0 or len(fl_data_files) == 0 or len(fl_image_files) == 0:
        raise Exception('Could not find files necessary to view reconstruction')
    emissivities = [np.load(f) for f in emissivity_files]
    fl_data = [scipy.io.idl.readsav(f) for f in fl_data_files]
    fl_images = [np.load(f) for f in fl_image_files]
    os.chdir(old_working_dir)

    frame_index = 0
    times = acquire.gpi_series(shot, 'phantom2', 'time')
    frames = acquire.video(shot, 'phantom2', sub=20, sobel=False)
    fl_rs = [f.fieldline_r for f in fl_data]
    fl_zs = [f.fieldline_z for f in fl_data]
    rlcfs, zlcfs = acquire.lcfs_rz(shot)
    efit_times, flux, flux_extent = acquire.time_flux_extent(shot)
    machine_x, machine_y = acquire.machine_cross_section()
    efit_t_index = process.find_nearest(efit_times, times[0], ordered=True)
    efit_phantom_start_index = efit_t_index
    previous_segments_frame_count = [sum([len(e) for e in emissivities[:t_index]]) for t_index in range(len(emissivities))]

    geomatrices = [np.transpose(np.array([fl.flatten() for fl in fl_image_set])) for fl_image_set in fl_images]
    reconstructed = geomatrices[0].dot(emissivities[0][0])
    reconstructed = reconstructed.reshape((64,64))

    fl_r_all = np.concatenate(fl_rs)
    fl_z_all = np.concatenate(fl_zs)
    r_space = np.linspace(np.min(fl_r_all), np.max(fl_r_all), 100)
    z_space = np.linspace(np.min(fl_z_all), np.max(fl_z_all), 100)
    r_grid, z_grid = np.meshgrid(r_space, z_space)
    emissivity_grid = make_grid(fl_rs[0], fl_zs[0], r_grid, z_grid, emissivities[0][0])
    
    # Draw figure using first frame
    fig, ax = plt.subplots()
    title = plt.suptitle('Shot {} frame {}'.format(shot, 0))
    plt.tight_layout(rect=(0, .1, 1, .9))

    plt.subplot(221)
    plt.title('Divertor camera view')
    plasma_image = plt.imshow(frames[0], cmap=plt.cm.gist_heat, origin='bottom')
    plt.axis('off')
    #plt.colorbar()
    
    plt.subplot(222)
    plt.title('Reconstruction')
    reconstruction_image = plt.imshow(reconstructed, cmap=plt.cm.gist_heat, origin='bottom')
    plt.axis('off')
    #plt.colorbar()
    
    plt.subplot(223)
    plt.title('Original minus reconstruction')
    error_image = plt.imshow(frames[0] - reconstructed, cmap=plt.cm.gist_heat, origin='bottom')
    plt.axis('off')
    #plt.colorbar()
    
    plt.subplot(224)
    plt.title('Toroidal cross section')
    emissivity_image = plt.pcolormesh(r_grid, z_grid, emissivity_grid, cmap=plt.cm.plasma)
    #colorbar = plt.colorbar()
    #colorbar.set_label('Relative emissivity')
    plt.axis('equal')
    plt.plot(machine_x, machine_y, color='gray')
    l, = plt.plot(rlcfs[efit_phantom_start_index], 
                  zlcfs[efit_phantom_start_index], color='orange', alpha=0.5)
    plt.xlim([.49, .62])
    plt.ylim([-.50, -.33])
    plt.xlabel('R (m)')
    plt.ylabel('Z (m)')
    plt.contour(flux[efit_t_index], 300, extent=flux_extent, alpha=0.5)
    plt.gca().set_axis_bgcolor('black')

    if not save:
        phantom_slide_area = plt.axes([0.20, 0.02, 0.60, 0.03])
        phantom_slider = Slider(phantom_slide_area, 'Camera frame', 0, len(frames)-1,
                            valinit=0)
        phantom_slider.valfmt = '%d'
        cutoff_slider_area = plt.axes([0.20, 0.05, 0.60, 0.03])
        cutoff_slider = Slider(cutoff_slider_area, 'Cutoff', 0, 100, valinit=0)
        cutoff_slider.valfmt = '%d%%'
        forward_button_area = plt.axes([0.95, 0.06, 0.04, 0.04])
        forward_button = Button(forward_button_area, '>')
        back_button_area = plt.axes([0.95, 0.01, 0.04, 0.04])
        back_button = Button(back_button_area, '<')

    def update_frame(val):
        global efit_t_index, frame_index
        frame_index = int(val)
        cutoff = cutoff_slider.val
#.........这里部分代码省略.........
开发者ID:sballin,项目名称:phantom_viewer,代码行数:103,代码来源:view.py

示例4: slide_correlation

# 需要导入模块: from matplotlib.widgets import Slider [as 别名]
# 或者: from matplotlib.widgets.Slider import valfmt [as 别名]
def slide_correlation(shot, fl_sav):
    """
    Slide through Phantom camera frames using given synthetic field line images 
    to create a reconstruction of the original using cross-correlation to find
    the best match.
    
    Args:
        shot: [int] shot number
        fl_sav: [scipy.io.idl.readsav object] containing field line images
    """
    time = acquire.gpi_series(shot, 'phantom2', 'time')
    frames = acquire.video(shot, 'phantom2', sub=20, sobel=False)
    frame_index = 0
    fls = fl_sav.fl_image
    fl_r = fl_sav.fieldline_r
    fl_z = fl_sav.fieldline_z
    rlcfs, zlcfs = acquire.lcfs_rz(shot)
    efit_times, flux, flux_extent = acquire.time_flux_extent(shot)
    efit_t_index = process.find_nearest(efit_times, time[0], ordered=True)
    machine_x, machine_y = acquire.machine_cross_section()

    # Find cross-correlation scores between frames and field line images
    xcorrs = np.zeros(fls.shape[0])
    for i, fl in enumerate(fls):
        xcorrs[i] = signals.cross_correlation(frames[frame_index], fl) 
    indices = np.argsort(xcorrs)

    # Interpolate field line cross-correlation scores over R, Z grid
    r_space = np.linspace(min(fl_r), max(fl_r), 100)
    z_space = np.linspace(min(fl_z), max(fl_z), 100)
    r_grid, z_grid = np.meshgrid(r_space, z_space)
    xcorr_grid = matplotlib.mlab.griddata(fl_r, fl_z, xcorrs, r_grid, z_grid, 
                                          interp='linear')

    # Plot camera image with field line overlay
    fig, ax = plt.subplots()
    fig.suptitle('Shot {}'.format(shot))

    plt.subplot(121)
    plt.title('Divertor camera view')
    plasma_image = plt.imshow(frames[frame_index], cmap=plt.cm.gray, 
                              origin='bottom')
    overlay_cmap = make_colormap([(1., 0., 0., 0.), (1., 0., 0., 1.)])
    fl_image = plt.imshow(fls[indices[-1]], cmap=overlay_cmap, origin='bottom',
                          alpha=0.8)
    plt.axis('off')

    # Plot field line R, Z data in context of machine
    plt.subplot(122)
    plt.title('Toroidal cross section')
    xcorr_image = plt.pcolormesh(r_grid, z_grid, xcorr_grid)
    colorbar = plt.colorbar()
    colorbar.set_label('Cross-correlation')
    plt.plot(machine_x, machine_y, color='gray')
    l, = plt.plot(rlcfs[efit_t_index], zlcfs[efit_t_index], color='fuchsia')
    plt.axis('equal')
    plt.xlim([.49, .62])
    plt.ylim([-.50, -.33])
    plt.xlabel('R (m)')
    plt.ylabel('Z (m)')
    f, = plt.plot(fl_r[indices[-1]], fl_z[indices[-1]], 'ro')
    plt.contour(flux[efit_t_index], 100, extent=flux_extent)
    
    # Slider and button settings
    fl_slide_area = plt.axes([0.20, 0.02, 0.60, 0.03])
    fl_slider = Slider(fl_slide_area, 'Correlation rank', 0, len(fls)-1, 
                       valinit=0)
    fl_slider.valfmt = '%d'
    phantom_slide_area = plt.axes([0.20, 0.06, 0.60, 0.03])
    phantom_slider = Slider(phantom_slide_area, 'Camera frame', 0, len(frames)-1,
                        valinit=0)
    phantom_slider.valfmt = '%d'
    forward_button_area = plt.axes([0.95, 0.06, 0.04, 0.04])
    forward_button = Button(forward_button_area, '>')
    back_button_area = plt.axes([0.95, 0.01, 0.04, 0.04])
    back_button = Button(back_button_area, '<')

    def update_data(val):
        global frame_index, indices, xcorr_grid
        frame_index = int(val)
        for i, fl in enumerate(fls):
            xcorrs[i] = signals.cross_correlation(frames[frame_index], fl) 
        xcorr_grid = matplotlib.mlab.griddata(fl_r, fl_z, xcorrs, r_grid, 
                                              z_grid, interp='linear')
        indices = np.argsort(xcorrs)[::-1]
        update_images(0)
    
    def update_images(val):
        global frame_index, indices, xcorr_grid
        val = int(val)
        efit_t_index = process.find_nearest(efit_times, time[frame_index], ordered=True)
        plasma_image.set_array(frames[frame_index])
        plasma_image.autoscale()
        fl_image.set_array(fls[indices[val]])
        fl_image.autoscale()
        f.set_xdata(fl_r[indices[val]])
        f.set_ydata(fl_z[indices[val]])
        l.set_xdata(rlcfs[efit_t_index])
        l.set_ydata(zlcfs[efit_t_index])
        xcorr_image.set_array(xcorr_grid[:-1, :-1].ravel())
#.........这里部分代码省略.........
开发者ID:sballin,项目名称:phantom_viewer,代码行数:103,代码来源:view.py

示例5: slide_reconstruct

# 需要导入模块: from matplotlib.widgets import Slider [as 别名]
# 或者: from matplotlib.widgets.Slider import valfmt [as 别名]
def slide_reconstruct(shot, fl_sav, smoothing_param=100):
    """
    Slide through Phantom camera frames using given synthetic field line images 
    to create a reconstruction of the original using non-negative least squares
    (NNLS) fitting. The NNLS method used here is slow, so there are long delays 
    between clicks and interface updates.
    
    Args:
        shot: [int] shot number
        fl_sav: [scipy.io.idl.readsav object] containing field line images
        smoothing_param: [float] least-squares smoothing parameter
    """
    time = acquire.gpi_series(shot, 'phantom2', 'time')
    frames = acquire.video(shot, 'phantom2', sub=20, sobel=False)
    frame_index = 0
    fl_images = fl_sav.fl_image
    fl_r = fl_sav.fieldline_r
    fl_z = fl_sav.fieldline_z
    rlcfs, zlcfs = acquire.lcfs_rz(shot)
    efit_times, flux, flux_extent = acquire.time_flux_extent(shot)
    efit_t_index = process.find_nearest(efit_times, time[0], ordered=True)
    machine_x, machine_y = acquire.machine_cross_section()

    inversion_func = scipy.optimize.nnls
    geomatrix = np.transpose(np.array([fl.flatten() for fl in fl_images]))
    geomatrix_smooth = np.concatenate((geomatrix, 
                                       smoothing_param*np.identity(len(fl_images))))
    target = frames[0]
    target_smooth = np.concatenate((target.flatten(), np.zeros(len(fl_images))))
    inv = inversion_func(geomatrix_smooth, target_smooth)

    r_space = np.linspace(min(fl_r), max(fl_r), 100)
    z_space = np.linspace(min(fl_z), max(fl_z), 100)
    r_grid, z_grid = np.meshgrid(r_space, z_space)
    emissivity_grid = matplotlib.mlab.griddata(fl_r, fl_z, inv[0], r_grid,
                                               z_grid, interp='linear') 

    reconstructed = geomatrix.dot(inv[0])
    reconstructed = reconstructed.reshape((64,64))
    target = target.reshape((64,64))
    fig, ax = plt.subplots()
    
    plt.subplot(221)
    plt.title('Divertor camera view')
    plasma_image = plt.imshow(target, cmap=plt.cm.gist_heat, origin='bottom')
    plt.axis('off')
    plt.colorbar()
    
    plt.subplot(222)
    plt.title('Reconstruction')
    reconstruction_image = plt.imshow(reconstructed, cmap=plt.cm.gist_heat, origin='bottom')
    plt.axis('off')
    plt.colorbar()
    
    plt.subplot(223)
    plt.title('Reconstruction minus original')
    error_image = plt.imshow(target - reconstructed, cmap=plt.cm.gist_heat, origin='bottom')
    plt.axis('off')
    plt.colorbar()
    
    plt.subplot(224)
    plt.title('Toroidal cross section')
    emissivity_image = plt.pcolormesh(r_grid, z_grid, emissivity_grid)
    colorbar = plt.colorbar()
    colorbar.set_label('Relative emissivity')
    plt.axis('equal')
    plt.plot(machine_x, machine_y, color='gray')
    l, = plt.plot(rlcfs[efit_t_index], zlcfs[efit_t_index], color='fuchsia')
    plt.xlim([.49, .62])
    plt.ylim([-.50, -.33])
    plt.xlabel('R (m)')
    plt.ylabel('Z (m)')
    plt.contour(flux[efit_t_index], 100, extent=flux_extent)

    phantom_slide_area = plt.axes([0.20, 0.02, 0.60, 0.03])
    phantom_slider = Slider(phantom_slide_area, 'Camera frame', 0, len(frames)-1,
                        valinit=0)
    phantom_slider.valfmt = '%d'
    forward_button_area = plt.axes([0.95, 0.06, 0.04, 0.04])
    forward_button = Button(forward_button_area, '>')
    back_button_area = plt.axes([0.95, 0.01, 0.04, 0.04])
    back_button = Button(back_button_area, '<')

    def update_data(val):
        global frame_index, emissivity_grid, reconstructed
        frame_index = int(val)
        target_smooth = np.concatenate((frames[frame_index].flatten(), np.zeros(len(fl_images))))
        inv = inversion_func(geomatrix_smooth, target_smooth)
        # inv = inversion_func(geomatrix, target)
        reconstructed = geomatrix.dot(inv[0]).reshape((64,64))
        emissivity_grid = matplotlib.mlab.griddata(fl_r, fl_z, inv[0], r_grid, 
                                              z_grid, interp='linear')
        plasma_image.set_array(frames[frame_index])
        reconstruction_image.set_array(reconstructed)
        emissivity_image.set_array(emissivity_grid[:-1, :-1].ravel())
        error_image.set_array(frames[frame_index]-reconstructed)
        error_image.autoscale()
        plasma_image.autoscale()
        reconstruction_image.autoscale()
        emissivity_image.autoscale()
#.........这里部分代码省略.........
开发者ID:sballin,项目名称:phantom_viewer,代码行数:103,代码来源:view.py


注:本文中的matplotlib.widgets.Slider.valfmt方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。