Skip to content

Instantly share code, notes, and snippets.

@joferkington
Created November 22, 2024 23:02
Show Gist options
  • Save joferkington/64b99219fe140ead26419aaea3909b9e to your computer and use it in GitHub Desktop.
Save joferkington/64b99219fe140ead26419aaea3909b9e to your computer and use it in GitHub Desktop.
Matplotlib colorbar example for Rob
import matplotlib.pyplot as plt
import numpy as np
# Make dummy data with different ranges...
datas = [np.random.random() * np.random.random((10, 10)) for _ in range(6)]
fig, axes = plt.subplots(ncols=len(datas), figsize=(30, 5))
for i, (data, ax) in enumerate(zip(datas, axes.flat)):
# It's important that we explicitly set vmin and vmax so they're the same
# for each image and we can have a single colorbar.
im = ax.imshow(data, vmin=0, vmax=1, cmap='magma')
ax.set(xticks=[], yticks=[], title=f"Dataset {i + 1}")
# We'll just use the last imshow artist from the loop as a "dummy"
# Note that we're passing in the full list of axes as `ax`! That means
# They will all be adjusted evenly to make space for the colorbar.
cbar = fig.colorbar(im, ax=axes, orientation='horizontal', fraction=0.05,
label='Image Values')
cbar.ax.xaxis.set(label_position='top')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment