为子图正确设置大小/放置共享颜色条

时间:2019-08-27 19:52:42

标签: python-3.x matplotlib matplotlib-basemap

我有横跨8行和1列的子图,我希望在最后一个子图的底部有一个共享的颜色条。但是,当我尝试执行此操作时,最后一个子图的大小发生了变化,使其离开了其上方的7个图。

如果我将这些颜色栏设置用于单个图,则一切看起来都很好。但是尝试将其用于8个子图,这给我带来了尺寸问题。为了节省空间,我将下面的代码截断了,但这显示了我对最后两个子图的了解。

from netCDF4 import Dataset as NetCDFFile
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
from mpl_toolkits.basemap import addcyclic

fig = plt.figure()

ax = fig.add_subplot(817)

nc = NetCDFFile('C:/Reanalysis Plots/netCDF files/phase7_olr_anom.nc')
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
time = nc.variables['time'][:]
olr = nc.variables['olr'][:]
olr,lon = addcyclic(olr,lon)
map = Basemap(llcrnrlon=0.,llcrnrlat=-40.,urcrnrlon=360.,urcrnrlat=40.,resolution='l')
lons,lats = np.meshgrid(lon,lat)
x,y = map(lons,lats)

levels = np.arange(-22.5,23.0,0.5)
levels = levels[levels!=0]
ticks = np.arange(-24.0,24.0,4.0)
plt.rcParams.update({'font.size': 5})
cs = map.contourf(x,y,olr[0],levels, cmap='bwr')
#cbar = plt.colorbar(cs, orientation='horizontal', cmap='bwr', spacing='proportional', ticks=ticks)
#cbar.set_label('Outgoing Longwave Radiation Anomalies $\mathregular{(W/m^2)}$')
map.drawcoastlines(linewidth=0.5)
yticks = map.drawparallels(np.arange(-20,21,20),labels=[1,0,0,0], linewidth=0.5, fontsize=4)
xticks = map.drawmeridians(np.arange(0,361,40),labels=[0,0,0,0], linewidth=0.5, fontsize=4)

plt.annotate('7', xy=(0, 1), xytext=(138, -25), fontsize=6,
    xycoords='axes fraction', textcoords='offset points',
    bbox=dict(facecolor='white', alpha=0.9, pad=1.5),
    horizontalalignment='center', verticalalignment='center')



ax = fig.add_subplot(818)


nc = NetCDFFile('C:/Reanalysis Plots/netCDF files/phase8_olr_anom.nc')
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
time = nc.variables['time'][:]
olr = nc.variables['olr'][:]
olr,lon = addcyclic(olr,lon)
map = Basemap(llcrnrlon=0.,llcrnrlat=-40.,urcrnrlon=360.,urcrnrlat=40.,resolution='l')
lons,lats = np.meshgrid(lon,lat)
x,y = map(lons,lats)

levels = np.arange(-22.5,23.0,0.5)
levels = levels[levels!=0]
ticks = np.arange(-24.0,24.0,4.0)
plt.rcParams.update({'font.size': 5})
cs = map.contourf(x,y,olr[0],levels, cmap='bwr')
cbar = plt.colorbar(cs, orientation='horizontal', cmap='bwr', spacing='proportional', ticks=ticks)
#cbar.set_label('Outgoing Longwave Radiation Anomalies $\mathregular{(W/m^2)}$')
map.drawcoastlines(linewidth=0.5)
yticks = map.drawparallels(np.arange(-20,21,20),labels=[1,0,0,0], linewidth=0.5, fontsize=4)
xticks = map.drawmeridians(np.arange(40,360,40),labels=[0,0,0,1], linewidth=0.5, fontsize=4)

plt.annotate('8', xy=(0, 1), xytext=(138, -25), fontsize=6,
    xycoords='axes fraction', textcoords='offset points',
    bbox=dict(facecolor='white', alpha=0.9, pad=1.5),
    horizontalalignment='center', verticalalignment='center')



plt.subplots_adjust(hspace=0.04)
plt.savefig('olr_multipanel.png',dpi=600)

OLR Plot

1 个答案:

答案 0 :(得分:2)

Matplotlib 2 Subplots, 1 Colorbar显示有关如何为多个子图创建一个颜色条的各种选项。在这里,将带有附加轴的gridspec用于颜色栏似乎很有用。

import matplotlib.pyplot as plt


fig, axes = plt.subplots(nrows=9, figsize=(6,9), 
                         gridspec_kw=dict(height_ratios=[1]*8+[0.2]))

fig.colorbar(plt.cm.ScalarMappable(), cax=axes[-1], orientation="horizontal")

plt.show()