【问题标题】:Proper sizing/placement of shared colorbar for subplots子图共享颜色条的正确大小/位置
【发布时间】:2019-08-27 19:52:42
【问题描述】:

我有跨越 8 行和 1 列的子图,我希望在最后一个子图的底部有一个共享颜色条。但是,当我尝试这样做时,最后一个子图的大小会发生变化,使其与上面的 7 个图相位移。

如果我将这些颜色条设置用于单个绘图,一切看起来都很好。但是尝试将它用于 8 个子图会给我带来尺寸问题。为了空间,我截断了下面的代码,但这显示了我对最后 2 个子图的内容。

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)

【问题讨论】:

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


    【解决方案1】:

    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()
    

    【讨论】:

      猜你喜欢
      • 2013-05-18
      • 2021-05-22
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 2023-03-13
      相关资源
      最近更新 更多