Skip to content
Snippets Groups Projects

Viz add colorbar

Merged s214743 requested to merge viz-add-colorbar into main
1 file
+ 12
5
Compare changes
  • Side-by-side
  • Inline
+ 12
5
@@ -137,6 +137,11 @@ def slices(
if isinstance(vol, da.core.Array):
vol = vol.compute()
if cbar:
# In this case, we want the vrange to be constant across the slices, which makes them all comparable to a single cbar.
new_vmin = vmin if vmin else np.min(vol)
new_vmax = vmax if vmax else np.max(vol)
# Run through each ax of the grid
for i, ax_row in enumerate(axs):
for j, ax in enumerate(np.atleast_1d(ax_row)):
@@ -144,10 +149,12 @@ def slices(
try:
slice_img = vol.take(slice_idxs[slice_idx], axis=axis)
# If vmin is higher than the highest value in the image ValueError is raised
# We don't want to override the values because next slices might be okay
new_vmin = None if (isinstance(vmin, (float, int)) and vmin > np.max(slice_img)) else vmin
new_vmax = None if (isinstance(vmax, (float, int)) and vmax < np.min(slice_img)) else vmax
if not cbar:
# If vmin is higher than the highest value in the image ValueError is raised
# We don't want to override the values because next slices might be okay
new_vmin = None if (isinstance(vmin, (float, int)) and vmin > np.max(slice_img)) else vmin
new_vmax = None if (isinstance(vmax, (float, int)) and vmax < np.min(slice_img)) else vmax
ax.imshow(
slice_img, cmap=cmap, interpolation=interpolation,vmin = new_vmin, vmax = new_vmax, **imshow_kwargs
)
@@ -185,7 +192,7 @@ def slices(
ax.axis("off")
if cbar:
norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax, clip=True)
norm = matplotlib.colors.Normalize(vmin=new_vmin, vmax=new_vmax, clip=True)
mappable = matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)
fig.colorbar(mappable=mappable, ax=np.atleast_1d(axs[0])[-1], orientation='vertical')
Loading