Skip to content

Instantly share code, notes, and snippets.

@blaylockbk
Last active March 22, 2023 14:20
Show Gist options
  • Save blaylockbk/e315c5e5aededb6aee6b08d1316f3d62 to your computer and use it in GitHub Desktop.
Save blaylockbk/e315c5e5aededb6aee6b08d1316f3d62 to your computer and use it in GitHub Desktop.
Adding colorbar on axes
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
data1 = np.random.random([5,5])
data2 = np.random.random([5,5])
fig = plt.figure(1, figsize=[16, 4])
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
this_plot = ax1.pcolormesh(data1, cmap='magma')
this_plot = ax2.pcolormesh(data2, cmap='magma')
divider = make_axes_locatable(ax2)
#cax = divider.append_axes('bottom', size='6%', pad=.25) # Method 1
cax = fig.add_axes([0.3, -0.0, 0.4, 0.05]) # Method 2, where the list is a rectangle [left, bottom, width, height]
cb = fig.colorbar(this_plot, cax=cax, orientation='horizontal')
cb.set_label('label and (units)')
@blaylockbk
Copy link
Author

blaylockbk commented Mar 22, 2023

Produces this figure:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment