[Matplotlib-users] Single Colorbar for multiple subplots

danjames95 danjames95 at live.co.uk
Wed Jul 19 10:31:51 EDT 2017


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.


More information about the Matplotlib-users mailing list