0

If I am generating the subplots in the way below, can I add a single colourbar for the whole figure?

fig = plt.figure(figsize=(60,35))
ax1 = fig.add_subplot(2,3,1)
ax2 = fig.add_subplot(2,3,2)
ax3  = fig.add_subplot(2,3,3)
ax4  = fig.add_subplot(2,3,4)
ax5  = fig.add_subplot(2,3,5)
plt.subplots_adjust(hspace=0.05,wspace=0.05)

I have seen answers to similar questions on stackoverflow(e.g. Matplotlib 2 Subplots, 1 Colorbar), but in those cases, the subplots are generated by something like

fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)

Currently, if I have to stick to my way of generating the subplots, how can I generate a single colourbar? Thanks.

Jeremy
  • 849
  • 6
  • 15

1 Answers1

0

As in the answer you link to you just need to create an axis object to hold the colorbar:

# e.g. from linked answer:
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
#                change ^^^^  ^^^^  ^^^^  ^^^^ to suit your needs
fig.colorbar(im, cax=cbar_ax)
compuphys
  • 1,289
  • 12
  • 28