Plotly: How to output multiple line charts in single figure?

给你一囗甜甜゛ 提交于 2020-05-15 22:42:48

问题


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()

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

Current output:

Desired Output:

Kindly help me with the code!


回答1:


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.

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.

Plot:

Complete code:

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

...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

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



来源:https://stackoverflow.com/questions/61747526/plotly-how-to-output-multiple-line-charts-in-single-figure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!