在子图中绘制为 cartopy 投影时,不会出现颜色条标签

问题描述 投票:0回答:1

颜色条看起来不错,但没有附带任何值。请参阅下面的代码。

代码绘制 2 列,每列有 4 行用于地图,另外一行用于颜色条。颜色条上的标签没有出现,我不知道为什么。

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)

python matplotlib projection colorbar cartopy
1个回答
3
投票
  • ax[4, 0]
    ax[4, 1]
    被指定为
    ccrs.PlateCarree
    投影,这会导致颜色条显示不正确。
  • 最简单的选择似乎是:
    1. 像使用
      gridspec
      一样创建子图,因此将为颜色条分配一个位置。
    2. 删除分配给颜色条的两个
      Axes
    3. 在同一位置添加新的子图,但不是投影。
    • 这是最简单的,因为它不需要更改现有的循环。
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])

# remove the Axes with a projection
ax[4, 0].remove()
ax[4, 1].remove()

# add new subplots that aren't projections
ax9 = fig.add_subplot(5, 2, 9)
ax10 = fig.add_subplot(5, 2, 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 = fig.colorbar(im1, cax=ax9, 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 = fig.colorbar(im2, cax=ax10, orientation='horizontal')
cbar2.ax.set_aspect(0.075)


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

# create subplot mosaic with different keyword arguments
fig, ax = plt.subplot_mosaic("AB;CD;EF;GH;IJ",
                             per_subplot_kw={('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'): {'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; iterate through the left Axes letters
for v, i in enumerate(['A', 'C', 'E', 'G']):

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

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

# tempestextreme plotting; iterate through the right Axes letters
for v, i in enumerate(['B', 'D', 'F', 'H']):

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

cbar2 = fig.colorbar(im2, cax=ax['J'], orientation='horizontal')
cbar2.ax.set_aspect(0.075)
© www.soinside.com 2019 - 2024. All rights reserved.