|
From: Benjamin R. <ben...@ou...> - 2012-12-13 15:57:21
|
On Thu, Dec 13, 2012 at 10:45 AM, Claus <cla...@gm...> wrote: > Hi, > > I am trying to plot a colorbar next to each subplot in a figure. In the > following example, I create two figures. In the second figure, I try to add > the colorbars. Is there a way to show the colorbar next to each subplot. > The way I did it, all the colorbars appear next to the last subplot, take > away space from it, and all are plotted using the "jet" colormap. > Unfortunately, I am not sure how to do this better, and would appreciate > hints. > > > import numpy as np > import matplotlib.pylab as plt > > def main(): > > # four subplots, no colorbar, so far so good > f, axarr = plt.subplots(2, 2) > axarr[0, 0].imshow(np.random.rand(5,5)*10) > axarr[0, 1].imshow(np.random.rand(5,5)) > axarr[1, 0].imshow(np.random.rand(5,5)*100) > axarr[1, 1].imshow(np.random.rand(5,5)*1000) > plt.show() > > # four subplots, four colorbars > f, axarr = plt.subplots(2, 2) > a = axarr[0, 0].imshow(np.random.rand(5,5)*10) > cbar1 = f.colorbar(a, cmap='jet') > b = axarr[0, 1].imshow(np.random.rand(5,5)) > cbar2 = f.colorbar(b, cmap='Reds') > c = axarr[1, 0].imshow(np.random.rand(5,5)*100) > cbar2 = f.colorbar(c, cmap='Blues') > d = axarr[1, 1].imshow(np.random.rand(5,5)*1000) > cbar2 = f.colorbar(d, cmap='Greens') > plt.show() > > if __name__ == '__main__': > main() > I think you are looking for the AxesGrid object from the mpl_toolkits.axes_grid1 module: http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html#axes-grid1 Ben Root |