【发布时间】:2020-07-23 15:42:33
【问题描述】:
我正在尝试使用 matplotlib 制作 3,2 子图,但在阅读文档后我不明白如何执行此操作,因为它适用于我的代码,如下所示:
import pandas as pd
from sys import exit
import numpy as np
import matplotlib.pyplot as plt
import datetime
import xarray as xr
import cartopy.crs as ccrs
import calendar
list = [0,1,2,3,4,5]
now = datetime.datetime.now()
currm = now.month
import calendar
fig, axes = plt.subplots(nrows=3,ncols=2)
fig.subplots_adjust(hspace=0.5)
fig.suptitle('Teleconnection Pos+ Phases {} 2020'.format(calendar.month_name[currm-1]))
#for x in list:
#for ax, x in zip(axs.ravel(), list):
for x, ax in enumerate(axes.flatten()):
dam = DS.where(DS['time.year']==rmax.iloc[x,1]).groupby('time.month').mean()#iterate by index
of column "1" or the years
dam = dam.sel(month=3)#current month mean 500
dam = dam.sel(level=500)
damc = dam.to_array()
lats = damc['lat'].data
lons = damc['lon'].data
#plot data
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines(lw=1)
damc = damc.squeeze()
ax.contour(lons,lats,damc,cmap='jet')
ax.set_title(tindices[x])
plt.show()
#plt.clf()
我已经尝试了多个选项,其中一些在 cmets 上面,但我无法让子图显示在我期待的 3,2 子图中。我只得到单个地块。我在下面的 for 循环中包含了第一个图,您可以看到它没有绘制在 3,2 子图区域内:
[![enter image description here][1]][1]
带有“ax.contour”的行可能是问题,但我不确定。非常感谢,下面是我的目标子图区域:
[![enter image description here][1]][1]
【问题讨论】:
-
请在代码顶部包含所有
import行,最好是reproducible example 的示例数据。另外,您对问题的描述不是很清楚。如果六个图呈现在一列上,问题是什么? -
对不起,我的代码很长,所以我的重点是 for 循环,我现在在顶部添加了几行。我已经编辑了这个问题,让我明白我根本没有得到子图——我总共只得到 6 个图,在循环中一次生成一个。希望这更清楚。谢谢
标签: python pandas loops matplotlib subplot