You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Cartopy投影绘制子图时Colorbar刻度标签不显示

问题:Colorbar可正常显示但无刻度数值

绘制2列4行的地图子图,额外一行用于放置Colorbar,但Colorbar上的刻度标签无法显示,代码及问题效果如下:

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter

# for labeling lat and lon
lon_formatter = LongitudeFormatter(zero_direction_label=True)
lat_formatter = LatitudeFormatter()

# lon and lat
lon = np.linspace(-180,180,360)
lat = np.linspace(0,90,90)

# initate figure
fig, ax = plt.subplots(5,2,subplot_kw={'projection':  ccrs.PlateCarree(central_longitude=180)}, \
                       gridspec_kw={'height_ratios': [0.33,0.33,0.33,0.33,0.05],'width_ratios': [1, 1], 'wspace': 0.1, 'hspace': 0.1},figsize=[15,10])

# mundhenk plotting
for i in range(0,4):

    # create map
    referenced_data = 20.0 * np.random.rand(90,360)
    im1 = ax[i,0].contourf(lon,lat,referenced_data,cmap='Blues',levels=np.linspace(0,20,6),extend='max')
    ax[i,0].coastlines()
    ax[i,0].set_xticks([0, 60, 120, 180, 240, 300, 360], crs=ccrs.PlateCarree())
    ax[i,0].set_yticks([0, 20, 40, 60, 80], crs=ccrs.PlateCarree())
    if i < 3:
        ax[i,0].set_xticklabels([])
        ax[i,0].yaxis.set_major_formatter(lat_formatter)
    else:
        ax[i,0].xaxis.set_major_formatter(lon_formatter)
        ax[i,0].yaxis.set_major_formatter(lat_formatter)
    ax[i,0].set_ylim(20,80)
    ax[i,0].set_xlim(-100,150)

# Add a colorbar for all subplots
cbar1 = plt.colorbar(im1, cax=ax[4,0], orientation='horizontal', label='(AR Day) / day')
cbar1.ax.set_aspect(0.075)

# tempestextreme plotting
for i in range(0,4):

    # create map
    referenced_data = 20.0 * np.random.rand(90,360)
    im2 = ax[i,1].contourf(lon,lat,referenced_data,cmap='Blues',levels=np.linspace(0,20,6),extend='max')
    ax[i,1].coastlines()
    ax[i,1].set_xticks([0, 60, 120, 180, 240, 300, 360], crs=ccrs.PlateCarree())
    ax[i,1].set_yticks([0, 20, 40, 60, 80], crs=ccrs.PlateCarree())
    if i < 3:
        ax[i,1].set_xticklabels([])
    else:
        ax[i,1].xaxis.set_major_formatter(lon_formatter)
    ax[i,1].set_yticklabels([])
    ax[i,1].set_ylim(20,80)
    ax[i,1].set_xlim(-100,150)

cbar2 = plt.colorbar(im2,cax=ax[4,1],orientation='horizontal')
cbar2.ax.set_aspect(0.075)

Colorbar无刻度标签问题展示

问题原因

代码中给Colorbar的轴设置了set_aspect(0.075),这个过小的宽高比会把水平方向的Colorbar刻度标签挤压到完全不可见。

解决方法

删除两行设置宽高比的代码即可恢复刻度显示,修改后的完整代码如下:

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter

# for labeling lat and lon
lon_formatter = LongitudeFormatter(zero_direction_label=True)
lat_formatter = LatitudeFormatter()

# lon and lat
lon = np.linspace(-180,180,360)
lat = np.linspace(0,90,90)

# initate figure
fig, ax = plt.subplots(5,2,subplot_kw={'projection':  ccrs.PlateCarree(central_longitude=180)}, \
                       gridspec_kw={'height_ratios': [0.33,0.33,0.33,0.33,0.05],'width_ratios': [1, 1], 'wspace': 0.1, 'hspace': 0.1},figsize=[15,10])

# mundhenk plotting
for i in range(0,4):

    # create map
    referenced_data = 20.0 * np.random.rand(90,360)
    im1 = ax[i,0].contourf(lon,lat,referenced_data,cmap='Blues',levels=np.linspace(0,20,6),extend='max')
    ax[i,0].coastlines()
    ax[i,0].set_xticks([0, 60, 120, 180, 240, 300, 360], crs=ccrs.PlateCarree())
    ax[i,0].set_yticks([0, 20, 40, 60, 80], crs=ccrs.PlateCarree())
    if i < 3:
        ax[i,0].set_xticklabels([])
        ax[i,0].yaxis.set_major_formatter(lat_formatter)
    else:
        ax[i,0].xaxis.set_major_formatter(lon_formatter)
        ax[i,0].yaxis.set_major_formatter(lat_formatter)
    ax[i,0].set_ylim(20,80)
    ax[i,0].set_xlim(-100,150)

# Add a colorbar for all subplots
cbar1 = plt.colorbar(im1, cax=ax[4,0], orientation='horizontal', label='(AR Day) / day')

# tempestextreme plotting
for i in range(0,4):

    # create map
    referenced_data = 20.0 * np.random.rand(90,360)
    im2 = ax[i,1].contourf(lon,lat,referenced_data,cmap='Blues',levels=np.linspace(0,20,6),extend='max')
    ax[i,1].coastlines()
    ax[i,1].set_xticks([0, 60, 120, 180, 240, 300, 360], crs=ccrs.PlateCarree())
    ax[i,1].set_yticks([0, 20, 40, 60, 80], crs=ccrs.PlateCarree())
    if i < 3:
        ax[i,1].set_xticklabels([])
    else:
        ax[i,1].xaxis.set_major_formatter(lon_formatter)
    ax[i,1].set_yticklabels([])
    ax[i,1].set_ylim(20,80)
    ax[i,1].set_xlim(-100,150)

cbar2 = plt.colorbar(im2,cax=ax[4,1],orientation='horizontal')

内容的提问来源于stack exchange,提问作者Joe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 16:05:53