[Matplotlib-users] Single Colorbar for multiple subplots

Klymak Jody jklymak at uvic.ca
Sat Aug 5 13:05:06 EDT 2017


I modified matplotlib to let you do this by passing an array of axes to the ax argument of colorbar.  It's in the git master branch but that has other bugs so you may not want to use it yet.  

But the way to do this with older releases is to make the axis you want to put the colorbar in manually (cbax=fig.add_axes) with the correct position and then pass cbax to colorbar as the argument to the parameter cax.  Of course you may need to make the other subplots smaller prob using the subplots_adjust method if the figure. 

Cheers.  Jody



Sent from my iPhone

> On Jul 19, 2017, at 07:31, danjames95 <danjames95 at live.co.uk> wrote:
> 
> Hi,
> 
> I am trying to plot 4 subplots in a 2 by 2 grid for mean sea level pressure
> data over the four seasons.
> 
> I have managed to plot all four in the same figure with their own individual
> colorbars, however, I would ideally like the figure to display one colorbar
> at the bottom to represent all four figures and I have been unsuccessful in
> doing this so far. There have been a few posts on this but as this is my
> first assignment (i'm a student at university) with python, I'm struggling
> to understand the answers! 
> 
> The section of my code which involves the plotting of data is as follows:
> 
> ...
> #Select Projection 
> lon_0= 0 
> lat_0=lats.mean()
> 
> m = Basemap(projection = 'cyl', lat_0=lat_0, lon_0=lon_0, resolution = 'l')
> #Convert Lons and Lats
> x,y = np.meshgrid (lons_shifted,lats)
> xx,yy= m(x,y)
> 
> v = np.linspace(980, 1030, 11, endpoint=True) 
> 
> fig = plt.figure()
> 
> plt.subplot(2, 2, 1)
> set1 = m.contourf(xx,yy,MSLP_WINTER, v)
> m.drawmapboundary()
> m.drawcoastlines()
> plt.title('Winter')
> bar = plt.colorbar(orientation = 'horizontal', ticks = v, format = '%.0f')
> plt.xlim(-80,20)
> plt.ylim (20,70)
> 
> plt.subplot(2, 2, 2)
> set2 = m.contourf(xx,yy,MSLP_SPRING, v)
> m.drawmapboundary()
> m.drawcoastlines()
> plt.title('Spring')
> bar = plt.colorbar(orientation = 'horizontal', ticks = v)
> plt.xlim(-80,20)
> plt.ylim (20,70)
> 
> plt.subplot(2, 2, 3)
> set3 = m.contourf(xx,yy,MSLP_SUMMER, v)
> m.drawmapboundary()
> m.drawcoastlines()
> plt.title('Summer')
> bar = plt.colorbar(orientation = 'horizontal', ticks = v)
> plt.xlim(-80,20)
> plt.ylim (20,70)
> 
> plt.subplot(2, 2, 4)
> set4 = m.contourf(xx,yy,MSLP_AUTUMN, v)
> m.drawmapboundary()
> m.drawcoastlines()
> plt.title('Autumn')
> bar = plt.colorbar(orientation = 'horizontal', ticks = v)
> plt.xlim(-80,20)
> plt.ylim (20,70)
> 
> plt.suptitle("Seasonally Averaged Mean Sea Level Pressure between 2006 to
> 2016")
> 
> plt.show()
> 
> This creates the following image: 
> <http://matplotlib.1069221.n5.nabble.com/file/n48091/MSLP_Seasonal_Averages.png> 
> 
> Any help would be very much appreciated.
> Also, if there any obvious bad habits within this code, please feel free to
> point them out! 
> 
> Thanks, 
> Dan 
> 
> 
> 
> --
> View this message in context: http://matplotlib.1069221.n5.nabble.com/Single-Colorbar-for-multiple-subplots-tp48091.html
> Sent from the matplotlib - users mailing list archive at Nabble.com.
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users



More information about the Matplotlib-users mailing list