Skip to content

Instantly share code, notes, and snippets.

@snsinfu
Created September 27, 2021 08:49
Show Gist options
  • Save snsinfu/ebc8c1d78f95fa215bc72f3e7d135d8c to your computer and use it in GitHub Desktop.
Save snsinfu/ebc8c1d78f95fa215bc72f3e7d135d8c to your computer and use it in GitHub Desktop.
Short colorbar
import matplotlib.pyplot as plt
import mpl_toolkits.axes_grid1.inset_locator
fig, ax = plt.subplots()
matrix = np.random.normal(size=(100, 100))
sm = ax.matshow(matrix)
# Attach a small axes to the lower right of the main axes. It's an "inset"
# placed outside of the plot. The size is 0.02 width x 0.5 height of the
# main axes.
cbar_ax = mpl_toolkits.axes_grid1.inset_locator.inset_axes(
ax,
width="100%", height="100%",
bbox_to_anchor=(1.02, 0, 0.02, 0.5), bbox_transform=ax.transAxes,
borderpad=0
)
cbar = fig.colorbar(sm, cax=cbar_ax)
# If you favor an unbordered colorbar.
cbar.outline.set_edgecolor("none")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment