10
   import xarray as xr     
   import cartopy.crs as ccrs


    USA_PROJ = ccrs.AlbersEqualArea(central_longitude=-97., central_latitude=38.)
    g_simple = ds_by_month.t2m.plot(x='longitude',
                                    y='latitude',
                                    col='month',
                                    col_wrap=6,
                                    aspect=ds.dims['longitude'] / ds.dims['latitude'],
                                    subplot_kws=dict(projection=USA_PROJ),
                                    add_colorbar=False,
                                    transform=ccrs.PlateCarree())
    g_simple.add_colorbar(orientation='horizontal')
    for ax in g_simple.axes.ravel():
        ax.coastlines()
        ax.set_extent([-121, -72, 22.5, 50])

    plt.tight_layout()
    plt.show()

在运行上面的代码时,我明白了。数字: 在此处输入图像描述

如何确保颜色条不与图重叠?即使我使用 xarray 默认颜色栏,也会发生重叠。

4

1 回答 1

9

您可以为颜色条提供自己的一组轴并将“底部”值设置为负值以使其超出边界框,或者使用关键字参数(即hspace = 2等)设置 subplots_adjust 函数。

下面是一个带有随机数据的示例(从matplotlib 子图示例修改):

import numpy as np
import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=2, ncols=6, figsize=(15,5))
for ax in axes.flat:
    im = ax.imshow(np.random.random((10,10)), vmin=0, vmax=1)

# color bar
fig.subplots_adjust(right=0.875) #also try using kwargs bottom, top, or hspace 
cbar_ax = fig.add_axes([0.1, -0.1, .8, .05]) #left, bottom, width, height
fig.colorbar(im, cax=cbar_ax, orientation="horizontal")

plt.show()

在此处输入图像描述

于 2018-04-06T09:25:25.833 回答