循环中有多个图-底图python

时间:2018-12-19 00:03:11

标签: python numpy matplotlib-basemap

我已经计算出某个时期的每月平均值,并希望生成12个图。在以下代码中,我仅尝试使用一月和二月。我尝试循环执行所有操作均失败。我该如何转换以下适合循环的python脚本?谢谢。

在地图上绘制

lon, lat = np.meshgrid(lons, lats)

xi, yi = map(lon, lat)

fig, axes = plt.subplots(1, 2)

axes[0].set_title("January")
map = Basemap(110.,-45.,155,-9., ax=axes[0],
            lat_0=24.75, lon_0=134.0, lat_1=-10, lat_2=-40,
            rsphere=(6378137.00,6356752.3142),
            projection='cyl')
map.drawcoastlines()
map.drawstates()
map.drawlsmask(land_color='Linen', ocean_color='#CCFFFF')
map.drawcountries()

parallels = np.arange(-50,-10,5.) # make latitude lines ever 5 degrees from 30N-50N
meridians = np.arange(115,155,5.) # make longitude lines every 5 degrees from 95W to 70W
map.drawparallels(parallels,labels=[1,0,0,0],fontsize=8)
map.drawmeridians(meridians,labels=[0,0,0,1],fontsize=8)
monthly_mean_data1 = np.ma.masked_where(np.ma.getmask(var_mask), all_monthly_average[0])
plot=map.pcolor(xi,yi,monthly_mean_data1)

axes[1].set_title("February")
map = Basemap(110.,-45.,155,-9., ax=axes[1],
            lat_0=24.75, lon_0=134.0, lat_1=-10, lat_2=-40,
            rsphere=(6378137.00,6356752.3142),
            projection='cyl')
map.drawcoastlines()
map.drawstates()
map.drawlsmask(land_color='Linen', ocean_color='#CCFFFF')
map.drawcountries()

parallels = np.arange(-50,-10,5.) # make latitude lines ever 5 degrees from 30N-50N
meridians = np.arange(115,155,5.) # make longitude lines every 5 degrees from 95W to 70W
map.drawparallels(parallels,labels=[1,0,0,0],fontsize=8)
map.drawmeridians(meridians,labels=[0,0,0,1],fontsize=8)
monthly_mean_data2 = np.ma.masked_where(np.ma.getmask(var_mask), all_monthly_average[1])
plot=map.pcolor(xi,yi,monthly_mean_data2)
plt.show()

1 个答案:

答案 0 :(得分:0)

谢谢!这是解决方案---

fig, axes = plt.subplots(1, 2)

axes = axes.ravel()

for i, Month in enumerate(['January', 'February']):
    map = Basemap(110.,-45.,155,-9., ax=axes[i],
            lat_0=24.75, lon_0=134.0, lat_1=-10, lat_2=-40,
            rsphere=(6378137.00,6356752.3142),
            projection='cyl')
    map.drawcoastlines()
    map.drawstates()
    map.drawlsmask(land_color='Linen', ocean_color='#CCFFFF')
    map.drawcountries()

    parallels = np.arange(-50,-10,5.) # make latitude lines ever 5 degrees from 30N-50N
    meridians = np.arange(115,155,5.) # make longitude lines every 5 degrees from 95W to 70W
    map.drawparallels(parallels,labels=[1,0,0,0],fontsize=8)
    map.drawmeridians(meridians,labels=[0,0,0,1],fontsize=8)
    monthly_mean_data= np.ma.masked_where(np.ma.getmask(var_mask), all_monthly_average[i])
    plot=map.pcolor(xi,yi,monthly_mean_data)
    axes[i].set_title(Month);