Commit 16ec364f authored by syz's avatar syz
Browse files

Combined plot_map with single_img_cbar_plot + added sdev option

parent babe0ca5
......@@ -14,7 +14,7 @@ import numpy as np
from IPython.display import display
from matplotlib import pyplot as plt
from .plot_utils import plot_loops, plot_map_stack, get_cmap_object, single_img_cbar_plot, save_fig_filebox_button, \
from .plot_utils import plot_loops, plot_map_stack, get_cmap_object, plot_map, save_fig_filebox_button, \
set_tick_font_size
from ..analysis.utils.be_loop import loop_fit_function
from ..analysis.utils.be_sho import SHOfunc
......@@ -310,7 +310,7 @@ def jupyter_visualize_beps_sho(h5_sho_dset, step_chan, resp_func=None, resp_labe
ax_bias.set_ylabel(step_chan.replace('_', ' ') + ' (V)')
bias_slider = ax_bias.axvline(x=step_ind, color='r')
img_map, img_cmap = single_img_cbar_plot(ax_map, spatial_map.T, show_xy_ticks=None)
img_map, img_cmap = plot_map(ax_map, spatial_map.T, show_xy_ticks=None)
map_title = '{} - {}={}'.format(sho_quantity, step_chan, bias_mat[step_ind][0])
ax_map.set_xlabel('X')
......@@ -415,7 +415,7 @@ def jupyter_visualize_be_spectrograms(h5_main, cmap=None):
spatial_map = np.abs(np.reshape(h5_main[:, 0], pos_dims[::-1]))
spectrogram = np.reshape(h5_main[0], (num_udvs_steps, -1))
fig, axes = plt.subplots(ncols=3, figsize=(12, 4), subplot_kw={'adjustable': 'box-forced'})
spatial_img, spatial_cbar = single_img_cbar_plot(axes[0], np.abs(spatial_map), x_size=spatial_map.shape[0],
spatial_img, spatial_cbar = plot_map(axes[0], np.abs(spatial_map), x_size=spatial_map.shape[0],
y_size=spatial_map.shape[1], cmap=cmap)
axes[0].set_aspect('equal')
axes[0].set_xlabel('X')
......@@ -424,13 +424,11 @@ def jupyter_visualize_be_spectrograms(h5_main, cmap=None):
crosshair = axes[0].plot(int(0.5 * spatial_map.shape[0]), int(0.5 * spatial_map.shape[1]), 'k+')[0]
if len(spec_dims) > 1:
amp_img, amp_cbar = single_img_cbar_plot(axes[1], np.abs(spectrogram), show_xy_ticks=None, cmap=cmap,
extent=[freqs_2d[0, 0], freqs_2d[-1, 0],
0, spectrogram.shape[0]])
amp_img, amp_cbar = plot_map(axes[1], np.abs(spectrogram), show_xy_ticks=None, cmap=cmap,
extent=[freqs_2d[0, 0], freqs_2d[-1, 0], 0, spectrogram.shape[0]])
phase_img, phase_cbar = single_img_cbar_plot(axes[2], np.angle(spectrogram), show_xy_ticks=None, cmap=cmap,
extent=[freqs_2d[0, 0], freqs_2d[-1, 0],
0, spectrogram.shape[0]])
phase_img, phase_cbar = plot_map(axes[2], np.angle(spectrogram), show_xy_ticks=None, cmap=cmap,
extent=[freqs_2d[0, 0], freqs_2d[-1, 0], 0, spectrogram.shape[0]])
for axis in axes[1:3]:
axis.set_ylabel('BE step')
......@@ -652,7 +650,7 @@ def jupyter_visualize_beps_loops(h5_projected_loops, h5_loop_guess, h5_loop_fit,
ax_map = plt.subplot2grid((1, 2), (0, 0), colspan=1, rowspan=1)
ax_loop = plt.subplot2grid((1, 2), (0, 1), colspan=1, rowspan=1)
im_map, im_cbar = single_img_cbar_plot(ax_map, spatial_map.T, x_size=spatial_map.shape[0],
im_map, im_cbar = plot_map(ax_map, spatial_map.T, x_size=spatial_map.shape[0],
y_size=spatial_map.shape[1], cmap=cmap)
ax_map.set_xlabel('X')
......@@ -962,7 +960,7 @@ def jupyter_visualize_loop_sho_raw_comparison(h5_loop_parameters, cmap=None):
ax_sho_phase = axes.flatten()[3]
# Plot the map of the loop parameters
plt_loop_map = single_img_cbar_plot(ax_loop_map, loop_parm_map, cmap=cmap)
plt_loop_map, loop_cbar = plot_map(ax_loop_map, loop_parm_map, cmap=cmap)
loop_vert_line = ax_loop_map.axvline(x=pos_dims[0], color='k')
loop_horz_line = ax_loop_map.axhline(y=pos_dims[1], color='k')
ax_loop_map.set_title(loop_map_title)
......@@ -1226,7 +1224,7 @@ def plot_loop_sho_raw_comparison(h5_loop_parameters, selected_loop_parm=None, se
ax_sho_phase = axes.flatten()[3]
# Plot the map of the loop parameters
loop_map, loop_map_cbar = single_img_cbar_plot(ax_loop_map, loop_parm_map)
loop_map, loop_map_cbar = plot_map(ax_loop_map, loop_parm_map)
crosshair = ax_loop_map.plot(selected_loop_pos[0], selected_loop_pos[1], 'k+')[0]
ax_loop_map.set_title(loop_map_title)
ax_loop_map.set_xlabel('X Position')
......
......@@ -8,7 +8,7 @@ from IPython.display import display
import ipywidgets as widgets
import numpy as np
from .plot_utils import single_img_cbar_plot
from .plot_utils import plot_map
def simple_ndim_visualizer(data_mat, pos_dim_names, pos_dim_units_old, spec_dim_names, spec_dim_units_old,
......@@ -169,9 +169,8 @@ def simple_ndim_visualizer(data_mat, pos_dim_names, pos_dim_units_old, spec_dim_
spec_titles = get_slice_string(spatmap_slicing, spec_dim_names, spec_ref_vals, spec_dim_units)
axes[0].set_title('Spatial Map for\n' + component_title + '\n' + spec_titles)
if pos_plot_2d:
img_spat, cbar_spat = single_img_cbar_plot(axes[0], current_spatmap,
x_size=data_mat.shape[1], y_size=data_mat.shape[0],
clim=clims)
img_spat, cbar_spat = plot_map(axes[0], current_spatmap, x_size=data_mat.shape[1], y_size=data_mat.shape[0],
clim=clims)
axes[0].set_xlabel(pos_dim_names[1] + ' (' + pos_dim_units_old[1] + ')')
axes[0].set_ylabel(pos_dim_names[0] + ' (' + pos_dim_units_old[0] + ')')
main_vert_line = axes[0].axvline(x=spgram_slicing[pos_dim_names[1]], color='k')
......@@ -192,10 +191,10 @@ def simple_ndim_visualizer(data_mat, pos_dim_names, pos_dim_units_old, spec_dim_
if spec_plot_2d:
axes[1].set_xlabel(spec_dim_names[1] + ' (' + spec_dim_units_old[1] + ')')
axes[1].set_ylabel(spec_dim_names[0] + ' (' + spec_dim_units_old[0] + ')')
img_spec, cbar_spec = single_img_cbar_plot(axes[1], current_spgram,
x_size=data_mat.shape[len(pos_dim_names) + 1],
y_size=data_mat.shape[len(pos_dim_names)],
cbar_label=component_name, clim=clims)
img_spec, cbar_spec = plot_map(axes[1], current_spgram,
x_size=data_mat.shape[len(pos_dim_names) + 1],
y_size=data_mat.shape[len(pos_dim_names)],
cbar_label=component_name, clim=clims)
else:
axes[1].set_xlabel(spec_xdim + ' (' + spec_dim_units[spec_xdim] + ')')
if current_spgram.shape[0] != spec_ref_vals[spec_xdim].size:
......
......@@ -380,51 +380,11 @@ def plot_line_family(axis, x_vec, line_family, line_names=None, label_prefix='',
_ = cbar_for_line_plot(axis, num_lines, **kwargs)
def plot_map(axis, data, stdevs=None, origin='lower', **kwargs):
"""
Plots a 2d map with a tight z axis, with or without color bars.
Note that the direction of the y axis is flipped if the color bar is required
Parameters
----------
axis : matplotlib.pyplot.axis object
Axis to plot this map onto
data : 2D real numpy array
Data to be plotted
stdevs : unsigned int (Optional. Default = None)
Number of standard deviations to consider for plotting. If None, full range is plotted.
origin : str
Where should the origin of the image data be located. 'lower' sets the origin to the
bottom left, 'upper' sets it to the upper left.
Default 'lower'
Returns
-------
"""
if stdevs is not None:
data_mean = np.mean(data)
data_std = np.std(data)
plt_min = data_mean - stdevs * data_std
plt_max = data_mean + stdevs * data_std
else:
plt_min = np.min(data)
plt_max = np.max(data)
im = axis.imshow(data, interpolation='none',
vmin=plt_min,
vmax=plt_max,
origin=origin,
**kwargs)
return im
def single_img_cbar_plot(axis, img, show_xy_ticks=True, show_cbar=True, x_size=1, y_size=1, num_ticks=4,
cbar_label=None, tick_font_size=14, **kwargs):
def plot_map(axis, img, show_xy_ticks=True, show_cbar=True, x_size=None, y_size=None, num_ticks=4,
stdevs=None, cbar_label=None, tick_font_size=14, origin='lower', **kwargs):
"""
Plots an image within the given axis with a color bar + label and appropriate X, Y tick labels.
This is particularly useful to get readily interpretable plots for papers
Parameters
----------
axis : matplotlib.axis object
......@@ -435,19 +395,24 @@ def single_img_cbar_plot(axis, img, show_xy_ticks=True, show_cbar=True, x_size=1
Whether or not to show X, Y ticks
show_cbar : bool, optional, default = True
Whether or not to show the colorbar
x_size : float, optional, default = 1
x_size : float, optional, default = number of pixels in x direction
Extent of tick marks in the X axis. This could be something like 1.5 for 1.5 microns
y_size : float, optional, default = 1
y_size : float, optional, default = number of pixels in y direction
Extent of tick marks in y axis
num_ticks : unsigned int, optional, default = 4
Number of tick marks on the X and Y axes
stdevs : unsigned int (Optional. Default = None)
Number of standard deviations to consider for plotting. If None, full range is plotted.
cbar_label : str, optional, default = None
Labels for the colorbar. Use this for something like quantity (units)
tick_font_size : unsigned int, optional, default = 14
Font size to apply to x, y, colorbar ticks and colorbar label
origin : str
Where should the origin of the image data be located. 'lower' sets the origin to the
bottom left, 'upper' sets it to the upper left.
Default 'lower'
kwargs : dictionary
Anything else that will be passed on to plot_map or imshow
Anything else that will be passed on to imshow
Returns
-------
im_handle : handle to image plot
......@@ -455,39 +420,35 @@ def single_img_cbar_plot(axis, img, show_xy_ticks=True, show_cbar=True, x_size=1
cbar : handle to color bar
handle to color bar
"""
if 'clim' not in kwargs:
im_handle = plot_map(axis, img, **kwargs)
else:
im_handle = axis.imshow(img, origin='lower', **kwargs)
if stdevs is not None:
data_mean = np.mean(img)
data_std = np.std(img)
kwargs.update({'clim': [data_mean - stdevs * data_std,
data_mean + stdevs * data_std]})
kwargs.update({'origin': origin})
im_handle = axis.imshow(img, **kwargs)
if show_xy_ticks is True:
x_ticks = np.linspace(0, img.shape[1] - 1, num_ticks, dtype=int)
y_ticks = np.linspace(0, img.shape[0] - 1, num_ticks, dtype=int)
axis.set_xticks(x_ticks)
axis.set_yticks(y_ticks)
axis.set_xticklabels([str(np.round(ind * x_size / (img.shape[1] - 1), 2)) for ind in x_ticks])
axis.set_yticklabels([str(np.round(ind * y_size / (img.shape[0] - 1), 2)) for ind in y_ticks])
set_tick_font_size(axis, tick_font_size)
elif show_xy_ticks is False:
if x_size is not None and y_size is not None:
x_ticks = np.linspace(0, img.shape[1] - 1, num_ticks, dtype=int)
y_ticks = np.linspace(0, img.shape[0] - 1, num_ticks, dtype=int)
axis.set_xticks(x_ticks)
axis.set_yticks(y_ticks)
axis.set_xticklabels([str(np.round(ind * x_size / (img.shape[1] - 1), 2)) for ind in x_ticks])
axis.set_yticklabels([str(np.round(ind * y_size / (img.shape[0] - 1), 2)) for ind in y_ticks])
set_tick_font_size(axis, tick_font_size)
else:
axis.set_xticks([])
axis.set_yticks([])
else:
set_tick_font_size(axis, tick_font_size)
cbar = None
if show_cbar:
# cbar = fig.colorbar(im_handle, ax=axis)
# divider = make_axes_locatable(axis)
# cax = divider.append_axes('right', size='5%', pad=0.05)
# cbar = plt.colorbar(im_handle, cax=cax)
cbar = plt.colorbar(im_handle, ax=axis, orientation='vertical',
fraction=0.046, pad=0.04, use_gridspec=True)
if cbar_label is not None:
cbar.set_label(cbar_label, fontsize=tick_font_size)
"""
z_lims = cbar.get_clim()
cbar.set_ticks(np.linspace(z_lims[0],z_lims[1], num_ticks))
"""
cbar.ax.tick_params(labelsize=tick_font_size)
return im_handle, cbar
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment