1

I would like to plot one subplot like:

Title 1
Fig1    Fig2    Fig3 

With a common colorbar for these 3 figures (1,2,3).

Title2
Fig4    Fig5    Fig6

With a common colorbar for these 3 figures (4,5,6).

I haven't found a way to add two different colorbars on the same figure as described above.

Hooked
  • 84,485
  • 43
  • 192
  • 261
user2050187
  • 175
  • 2
  • 5
  • 14

1 Answers1

4

It's a bit tricky, but you can share sets of subplots with a common colorbar.

enter image description here

I've drawn on a few previous anwers that might be worth reading as well:

Matplotlib 2 Subplots, 1 Colorbar

How can I create a standard colorbar for a series of plots in python

And of course, the documentation for matplotlib:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid

# Generate some random data
data_top = np.random.random((3,10,10)) * 5
data_bot = np.random.random((3,20,20)) * 10

fig  = plt.figure()

grid_top = ImageGrid(fig, 211, nrows_ncols = (1, 3),
                     cbar_location = "right",                     
                     cbar_mode="single",
                     cbar_pad=.2) 
grid_bot = ImageGrid(fig, 212, nrows_ncols = (1, 3),
                     cbar_location = "right",                     
                     cbar_mode="single",
                     cbar_pad=.2) 

for n in xrange(3):
    im1 = grid_top[n].imshow(data_top[n], 
                            interpolation='nearest', vmin=0, vmax=5)

    im2 = grid_bot[n].imshow(data_bot[n], cmap=plt.get_cmap('bone'), 
                             interpolation='nearest', vmin=0, vmax=10)


grid_top.cbar_axes[0].colorbar(im1)
grid_bot.cbar_axes[0].colorbar(im2)

plt.show()
Community
  • 1
  • 1
Hooked
  • 84,485
  • 43
  • 192
  • 261
  • Thank you for this help. It almost works.. I use basemap to plot orthographic projection. So , I have to define m=Basemap(projection='ortho',lon_0=lon_0,lat_0=lat_0,resolution='l',\llcrnrx=0.,llcrnry=0.,urcrnrx=m1.urcrnrx/2.,urcrnry=m1.urcrnry/2.) An then I call m.pcolor to plot my figures. But if I try im1 =grid_top[n].m.pcolor, it doesn't work. Also, how can I add a title above each figures and remove all the numers around the plots ? – user2050187 Jul 17 '13 at 10:14
  • @user2050187 Glad it helped a bit. StackOverflow works better if you break your questions into single bits that we can answer individually. It also helps if you state as much information as you can (i.e. Basemaps and polar plots) since it may have an impact on the answer. Removing the numbers and adding a common title are questions that have already been answered here on this site e.g. http://stackoverflow.com/questions/2176424/hiding-axis-text-in-matplotlib-plots, http://stackoverflow.com/questions/5812169/draw-text-inside-pylab-figure-window. Remember to accept an answer to your questions! – Hooked Jul 17 '13 at 13:50