颜色条看起来不错,但没有附带任何值。请参阅下面的代码。
代码绘制 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)
ax[4, 0]
和 ax[4, 1]
被指定为 ccrs.PlateCarree
投影,这会导致颜色条显示不正确。
gridspec
一样创建子图,因此将为颜色条分配一个位置。Axes
。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)
matplotlib.pyplot.subplot_mosaic
可用于创建每轴子图关键字参数。
Axes
对象Axes
对象# 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)