使用 subplot 和 groupby 绘制多个 y 值 [英] Plotting with multiple y values with subplot and groupby

查看:50
本文介绍了使用 subplot 和 groupby 绘制多个 y 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 df .头部请参见以下内容:

I have a df. See below for the head:

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') 我想使用 绘制 suspected caseconfirmed case 的图 xaxis 的日期列.将它们绘制为(5,2)个子图

By using df.groupby('Country') I want to plot the suspected case against the confirmed case using the Date column for the xaxis. Plotting these as (5, 2) subplots

到目前为止我所做的还没有完全明白:

What I've done so far hasn't quite got it yet:

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 子图,下面分别显示每个图.这是为什么?

What's happened so far is that there is an empty 5x2 subplot and below displays each graph individually. Why is this?

也只是一个小问题,但需要澄清一下.在我的 .plot()函数中,为什么最后一个分组的国家/地区仅显示在标题中?例如,我将 10 个国家/地区组合在一起用于此图,但标题 Suspected vs. Confirmed cases USA 显示每个图表.

Also just a minor issue but would like some clarification. Within my .plot() function, why is the last grouped country only being shown in the title? For instance I have 10 countries grouped together for this plot but the title Suspected vs. Confirmed cases USA is showing fr each graph.

看过一些SO帖子,并结合了一些答案以尝试解决我的问题,但我似乎是在盘旋.

Looked at a few SO posts and combined a few answers to try to solve my problem but I seem to be going in circles.

推荐答案

您可以:

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)

输出:

这篇关于使用 subplot 和 groupby 绘制多个 y 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆