Pandas 使用多个y值与子地块和groupby进行打印
我有一个Pandas 使用多个y值与子地块和groupby进行打印,pandas,matplotlib,pandas-groupby,subplot,Pandas,Matplotlib,Pandas Groupby,Subplot,我有一个df。头部见下文: Country Date suspected case confirmed cases suspected deaths confirmed deaths 0 Guinea 2014-08-29 25.0 141.0 482.0 648.0 1 Nigeria 2014-08-29 3.0
df
。头部见下文:
Country Date suspected case confirmed cases suspected deaths confirmed deaths
0 Guinea 2014-08-29 25.0 141.0 482.0 648.0
1 Nigeria 2014-08-29 3.0 1.0 15.0 19.0
2 Liberia 2014-08-29 382.0 674.0 322.0 1378.0
通过使用df.groupby('Country')
我想使用xaxis
的Date
列,将疑似病例
与确诊病例
进行对比。将这些绘制为(5,2)子图
到目前为止,我所做的还没有完全做到:
fig, ax = plt.subplots(2, 5, sharey=True)
df.groupby('Country').plot(x='Date', y=['suspected cases', 'confirmed cases'], title=f'Suspected vs. Confirmed cases {country}')
plt.show()
到目前为止,出现了一个空的5x2子图,下面分别显示每个图形。为什么会这样
也只是一个小问题,但希望得到一些澄清。在我的.plot()
函数中,为什么最后一个分组的国家/地区只显示在标题中?例如,本图将10个国家分组,但标题疑似病例与确诊病例美国
显示了每个图表
查看了一些SO帖子,并结合了一些答案试图解决我的问题,但我似乎在兜圈子。你可以:
fig, ax = plt.subplots(2, 5, sharey=True)
for (c,d), a in zip(df.groupby('Country'), ax.ravel()):
d.plot(x='Date',
y=['suspected cases', 'confirmed cases'],
title=f'Suspected vs. Confirmed cases {c}',
ax=a, subplots=False)
输出:
那么你有10个国家,想把每个国家划分成单独的子地块吗?@QuangHoang-Yup!