情节:如何在单个图形中输出多个折线图? [英] Plotly: How to output multiple line charts in single figure?

查看:110
本文介绍了情节:如何在单个图形中输出多个折线图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对单个图形中的多个数据框使用plotly绘制折线图. 我的代码是:

I am trying to plot line chart using plotly for multiple dataframes in a single graph. My code is:

import plotly.express as px
labels=category_names[:10]
for category in category_names[:10]:
    df_b=df1[df1['Country/Region']==category]    
    fig=px.line(df_b, x="Date", y="Confirmed",labels="Country/Region") 
    print(category)    
fig.show()

但是,通过使用上面的代码,我仅能获得for循环的最后一次迭代的折线图.

However, by using the above code I am just able to get the line graph for last iteration of for loop.

当前输出:

所需的输出:

请帮助我编写代码!

推荐答案

plotly.expresspx.line()结合使用,只要在单个图形中输出多行,就不必使用for loop.您的数据集为long格式.您可能会混淆使用for loopfig.add_figure()的这种方法,可以说它更适合wide格式的数据,在这种情况下,您会将国家/地区作为列名,将时间作为索引,并将值设为单个类别在您的数据框中.

Using plotly.express with px.line(), you shouldn't have to use a for loop at all to output multiple lines in a single figure as long as your dataset is of a long format. You might be confusing this approach to using a for loop and fig.add_figure(), which is arguably better suited for data of a wide format where you would have countries as column names, time as index, and a value of a single category in your dataframe.

如果没有适当的数据样本,很难100%地确定问题所在.但是在我看来,您的数据结构与px.data.gapminder()

Without a proper data sample it's not easy to tell with a 100% certainty what your issue is. But it seems to me that your data structure matches the structure of px.data.gapminder()

    country continent   year    lifeExp pop         gdpPercap   iso_alpha   iso_num
0   Afghanistan Asia    1952    28.801  8425333     779.445314  AFG 4
1   Afghanistan Asia    1957    30.332  9240934     820.853030  AFG 4
2   Afghanistan Asia    1962    31.997  10267083    853.100710  AFG 4
3   Afghanistan Asia    1967    34.020  11537966    836.197138  AFG 4
4   Afghanistan Asia    1972    36.088  13079460    739.981106  AFG 4

因此,我将在此基础上提供一个答案,您可以尝试从那里进行排序.当然,除非您愿意共享完整的数据样本和代码片段.

So I'll provide an answer based on that and you can try and sort it out from there. Unless you're willing to share a complete data sample and code snippet, of course.

情节:

完整代码:

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

# sample dataset from plotly express
df = px.data.gapminder()

# Filter and pivot dataset for each country,
# and add lines for each country
fig = go.Figure()
for c in df['country'].unique()[:3]:
    dfp = df[df['country']==c].pivot(index='year', columns='country', values='pop') 
    fig.add_traces(go.Scatter(x=dfp.index, y=dfp[c], mode='lines', name = c))

fig.show()

此代码段的作用是将源划分为每个唯一的类别,例如:

What this snippet does, is to subset the source into each unique category like:

    country continent   year    lifeExp pop gdpPercap   iso_alpha   iso_num
564 Germany Europe  1952    67.5    69145952    7144.114393 DEU 276
565 Germany Europe  1957    69.1    71019069    10187.826650    DEU 276
566 Germany Europe  1962    70.3    73739117    12902.462910    DEU 276
567 Germany Europe  1967    70.8    76368453    14745.625610    DEU 276
568 Germany Europe  1972    71.0    78717088    18016.180270    DEU 276

...并使用df[df['country']=='Germany'].pivot(index='year', columns='country', values='pop')旋转该数据集以获取:

...and pivot that dataset using df[df['country']=='Germany'].pivot(index='year', columns='country', values='pop') to get:

country Germany
year    
1952    69145952
1957    71019069
1962    73739117
1967    76368453
1972    78717088
1977    78160773
1982    78335266
1987    77718298
1992    80597764
1997    82011073
2002    82350671
2007    82400996

...然后然后使用fig.add_traces()将该数据添加到绘图中.

...and then add that data to a plotly figure using fig.add_traces() .

这篇关于情节:如何在单个图形中输出多个折线图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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