使用Basemap时,我遇到了子图的问题。我正在绘制卫星在六个压力水平下测量的相对湿度(RH)。在第一张图片(图1)中,我绘制了在卫星的整个轨迹上测量的RH(环绕地球)。每个子图对应一个压力水平。这个数字很好。然而,为了研究某个测量事件(这里:在马达加斯加东部看到的热带气旋),我想放大这个数字。放大(限制纬度和纵向范围)似乎也很好(图2)。
但是,在图2中,子图不能使用整个图形空间。我想如果子图使用了图的整个宽度,图2中所见的所有分量(即地图和测量的RH)都会被放大。换句话说,我希望图2中的子图与图1中的子图一样宽。请问,我该怎么做?
这是我的代码:
首先我加载我的文件和变量
然后是绘图:
import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.basemap import Basemap
from matplotlib import gridspec
# Generate variables
lat_use = np.arange(29.5, 30.5, 1)
lon_use = np.arange(179.5, 180.5, 1)
RH = np.random.random((60, 360, 6))
plt.subplot(611)
m = Basemap(projection='cyl', llcrnrlon=-180, urcrnrlon=180, llcrnrlat=-29.5, urcrnrlat=29.5, resolution='c')
m.drawcoastlines(color='k')
lons, lats = np.meshgrid(lon_use,lat_use)
x, y = m(lons,lats)
m.contourf(x, y, RH[:,:,5], cmap='RdYlBu')
plt.xlim(-179.5, 179.5)
plt.xticks([-180, -120, -60, 0, 60, 120, 180], ['180W', '120W', '60W', '0E', '60E', '120E', '180E'], fontsize=10)
plt.ylim(-29.5, 29.5)
plt.yticks([-30, -20, -10, 0, 10, 20, 30], ['30S', '20S', '10S', '0', '10N', '20N', '30N'], fontsize=10)
plt.ylabel('6th')
plt.title('RH in 6 levels')
#plt.show()
plt.subplot(612)
m = Basemap(projection='cyl', llcrnrlon=-180, urcrnrlon=180, llcrnrlat=-29.5, urcrnrlat=29.5, resolution='c')
m.drawcoastlines(color='k')
lons, lats = np.meshgrid(lon_use,lat_use)
x, y = m(lons,lats)
m.contourf(x, y, RH[:,:,4], cmap='RdYlBu')
plt.xlim(-179.5, 179.5)
plt.xticks([-180, -120, -60, 0, 60, 120, 180], ['180W', '120W', '60W', '0E', '60E', '120E', '180E'], fontsize=10)
plt.ylim(-29.5, 29.5)
plt.yticks([-30, -20, -10, 0, 10, 20, 30], ['30S', '20S', '10S', '0', '10N', '20N', '30N'], fontsize=10)
plt.ylabel('5th')
#plt.show()
plt.subplot(613)
m = Basemap(projection='cyl', llcrnrlon=-180, urcrnrlon=180, llcrnrlat=-29.5, urcrnrlat=29.5, resolution='c')
m.drawcoastlines(color='k')
lons, lats = np.meshgrid(lon_use,lat_use)
x, y = m(lons,lats)
m.contourf(x, y, RH[:,:,3], cmap='RdYlBu')
plt.xlim(-179.5, 179.5)
plt.xticks([-180, -120, -60, 0, 60, 120, 180], ['180W', '120W', '60W', '0E', '60E', '120E', '180E'], fontsize=10)
plt.ylim(-29.5, 29.5)
plt.yticks([-30, -20, -10, 0, 10, 20, 30], ['30S', '20S', '10S', '0', '10N', '20N', '30N'], fontsize=10)
plt.ylabel('4th')
#plt.show()
plt.subplot(614)
m = Basemap(projection='cyl', llcrnrlon=-180, urcrnrlon=180, llcrnrlat=-29.5, urcrnrlat=29.5, resolution='c')
m.drawcoastlines(color='k')
lons, lats = np.meshgrid(lon_use,lat_use)
x, y = m(lons,lats)
m.contourf(x, y, RH[:,:,2], cmap='RdYlBu')
plt.xlim(-179.5, 179.5)
plt.xticks([-180, -120, -60, 0, 60, 120, 180], ['180W', '120W', '60W', '0E', '60E', '120E', '180E'], fontsize=10)
plt.ylim(-29.5, 29.5)
plt.yticks([-30, -20, -10, 0, 10, 20, 30], ['30S', '20S', '10S', '0', '10N', '20N', '30N'], fontsize=10)
plt.ylabel('3rd')
#plt.show()
plt.subplot(615)
m = Basemap(projection='cyl', llcrnrlon=-180, urcrnrlon=180, llcrnrlat=-29.5, urcrnrlat=29.5, resolution='c')
m.drawcoastlines(color='k')
lons, lats = np.meshgrid(lon_use,lat_use)
x, y = m(lons,lats)
m.contourf(x, y, RH[:,:,1], cmap='RdYlBu')
plt.xlim(-179.5, 179.5)
plt.xticks([-180, -120, -60, 0, 60, 120, 180], ['180W', '120W', '60W', '0E', '60E', '120E', '180E'], fontsize=10)
plt.ylim(-29.5, 29.5)
plt.yticks([-30, -20, -10, 0, 10, 20, 30], ['30S', '20S', '10S', '0', '10N', '20N', '30N'], fontsize=10)
plt.ylabel('2nd')
#plt.show()
plt.subplot(616)
m = Basemap(projection='cyl', llcrnrlon=-180, urcrnrlon=180, llcrnrlat=-29.5, urcrnrlat=29.5, resolution='c')
m.drawcoastlines(color='k')
lons, lats = np.meshgrid(lon_use,lat_use)
x, y = m(lons,lats)
m.contourf(x, y, RH[:,:,0], cmap='RdYlBu')
plt.xlim(-179.5, 179.5)
plt.xticks([-180, -120, -60, 0, 60, 120, 180], ['180W', '120W', '60W', '0E', '60E', '120E', '180E'], fontsize=10)
plt.ylim(-29.5, 29.5)
plt.yticks([-30, -20, -10, 0, 10, 20, 30], ['30S', '20S', '10S', '0', '10N', '20N', '30N'], fontsize=10)
plt.ylabel('1st')
plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9)
cax = plt.axes([0.85, 0.1, 0.075, 0.8])
plt.colorbar(cax=cax, label='RH [%]')
plt.show()
# Plot RH in the six vertical levels with Basemap:
# But only in the interval 30E-70E and 0S-30S
# Define limits
lon_beg = 220
lon_end = 261
lat_beg = 30
lat_end = 60
lons_lim = lons[lat_beg:lat_end,lon_beg:lon_end]
lats_lim = lats[lat_beg:lat_end,lon_beg:lon_end]
plt.subplot(611)
m = Basemap(projection='cyl', llcrnrlon=40, urcrnrlon=80, llcrnrlat=-29.5, urcrnrlat=0.5, resolution='c')
m.drawcoastlines(color='k')
lons, lats = np.meshgrid(lon_use,lat_use)
x, y = m(lons_lim,lats_lim)
m.contourf(x, y, RH[lat_beg:lat_end,lon_beg:lon_end,5], cmap='RdYlBu')
plt.xlim(40.5, 80.5)
plt.xticks([40, 50, 60, 70, 80], ['40E', '50E', '60E', '70E', '80E'], fontsize=10)
plt.ylim(-29.5, 0.5)
plt.yticks([-30, -20, -10, 0], ['30S', '20S', '10S', '0'], fontsize=10)
plt.ylabel('6th')
plt.title('RH in 6 levels: Zoomed In')
#plt.show()
plt.subplot(612)
m = Basemap(projection='cyl', llcrnrlon=40, urcrnrlon=80, llcrnrlat=-29.5, urcrnrlat=0.5, resolution='c')
m.drawcoastlines(color='k')
lons, lats = np.meshgrid(lon_use,lat_use)
x, y = m(lons_lim,lats_lim)
m.contourf(x, y, RH[lat_beg:lat_end,lon_beg:lon_end,4], cmap='RdYlBu')
plt.xlim(40.5, 80.5)
plt.xticks([40, 50, 60, 70, 80], ['40E', '50E', '60E', '70E', '80E'], fontsize=10)
plt.ylim(-29.5, 0.5)
plt.yticks([-30, -20, -10, 0], ['30S', '20S', '10S', '0'], fontsize=10)
plt.ylabel('5th')
#plt.show()
plt.subplot(613)
m = Basemap(projection='cyl', llcrnrlon=40, urcrnrlon=80, llcrnrlat=-29.5, urcrnrlat=0.5, resolution='c')
m.drawcoastlines(color='k')
lons, lats = np.meshgrid(lon_use,lat_use)
x, y = m(lons_lim,lats_lim)
m.contourf(x, y, RH[lat_beg:lat_end,lon_beg:lon_end,3], cmap='RdYlBu')
plt.xlim(40.5, 80.5)
plt.xticks([40, 50, 60, 70, 80], ['40E', '50E', '60E', '70E', '80E'], fontsize=10)
plt.yticks([-30, -20, -10, 0], ['30S', '20S', '10S', '0'], fontsize=10)
plt.ylabel('4th')
#plt.show()
plt.subplot(614)
m = Basemap(projection='cyl', llcrnrlon=40, urcrnrlon=80, llcrnrlat=-29.5, urcrnrlat=0.5, resolution='c')
m.drawcoastlines(color='k')
lons, lats = np.meshgrid(lon_use,lat_use)
x, y = m(lons_lim,lats_lim)
m.contourf(x, y, RH[lat_beg:lat_end,lon_beg:lon_end,2], cmap='RdYlBu')
plt.xlim(40.5, 80.5)
plt.xticks([40, 50, 60, 70, 80], ['40E', '50E', '60E', '70E', '80E'], fontsize=10)
plt.ylim(-29.5, 0.5)
plt.yticks([-30, -20, -10, 0], ['30S', '20S', '10S', '0'], fontsize=10)
plt.ylabel('3rd')
#plt.show()
plt.subplot(615)
m = Basemap(projection='cyl', llcrnrlon=40, urcrnrlon=80, llcrnrlat=-29.5, urcrnrlat=0.5, resolution='c')
m.drawcoastlines(color='k')
lons, lats = np.meshgrid(lon_use,lat_use)
x, y = m(lons_lim,lats_lim)
m.contourf(x, y, RH[lat_beg:lat_end,lon_beg:lon_end,1], cmap='RdYlBu')
plt.xlim(40.5, 80.5)
plt.xticks([40, 50, 60, 70, 80], ['40E', '50E', '60E', '70E', '80E'], fontsize=10)
plt.ylim(-29.5, 0.5)
plt.yticks([-30, -20, -10, 0], ['30S', '20S', '10S', '0'], fontsize=10)
plt.ylabel('2nd')
#plt.show()
plt.subplot(616)
m = Basemap(projection='cyl', llcrnrlon=40, urcrnrlon=80, llcrnrlat=-29.5, urcrnrlat=0.5, resolution='c')
m.drawcoastlines(color='k')
lons, lats = np.meshgrid(lon_use,lat_use)
x, y = m(lons_lim,lats_lim)
m.contourf(x, y, RH[lat_beg:lat_end,lon_beg:lon_end,0], cmap='RdYlBu')
plt.xlim(40.5, 80.5)
plt.xticks([40, 50, 60, 70, 80], ['40E', '50E', '60E', '70E', '80E'], fontsize=10)
plt.ylim(-29.5, 0.5)
plt.yticks([-30, -20, -10, 0], ['30S', '20S', '10S', '0'], fontsize=10)
plt.ylabel('1st')
plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9)
cax = plt.axes([0.85, 0.1, 0.075, 0.8])
plt.colorbar(cax=cax, label='RH [%]')
plt.show()
图1.在六个压力水平(六个子图)中绘制的RH。
图2.基本上图1放大了。在六个压力水平(六个子图)中绘制的RH在马达加斯加周围放大。问题在于,在该图中,子图不会使用图的整个宽度。它们应该与图1中的子图具有相同的宽度。