matplotlib 自定义xarray颜色条位置

dm7nw8vv  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(120)

我尝试将colorbar定位在设计好的位置,如下图所示(第5行水平,从第二列开始)。
下面是演示代码

import cartopy.crs as ccrs
import numpy as np
import xarray as xr
# Make some random data
annual=xr.DataArray(np.random.randint(1,100,(41,186,469)), dims=("year","y","x"), coords={"year":np.arange(1981,2022),
                                                                                         "y":np.linspace(29,38,186),
                                                                                         "x":np.linspace(-10,10,469)})
# Plotting
proj=ccrs.PlateCarree()
fig=annual.plot(col="year",col_wrap=10, cmap="Spectral", vmin=1, vmax=105,
                subplot_kws={"projection":proj, "aspect":2.5})
# Increase title size
years=[f"Year = {i}" for i in range(1981,2022)]

for ax, year in zip(fig.axes.flatten(), years):
    ax.set_title(year,fontsize=15)
    ax.set_extent([-10, 10, 29, 38])
    ax.coastlines(linewidth=0.5)
    
fig.cbar.ax.tick_params(labelsize=30)
fig.cbar.set_label(label='Label', size=30, weight='bold')

3htmauhk

3htmauhk1#

我觉得你要找的是

fig = annual.plot(col="year", col_wrap=10, 
                  cmap="Spectral", vmin=1, vmax=105,
                  subplot_kws={"projection": proj, "aspect": 2.5},
                  cbar_kwargs={"location": "bottom"},
)

相关问题