Plotly:如何在单个图形中输出多个折线图?
Plotly: How to output multiple line charts in single figure?
我正在尝试使用 plotly 为单个图形中的多个数据框绘制折线图。
我的代码是:
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 循环最后一次迭代的折线图。
当前输出:
期望的输出:
请帮我写代码!
将 plotly.express
与 px.line()
结合使用,只要您的数据集是 long
格式。您可能会混淆这种使用 for loop
和 fig.add_figure()
的方法,这可以说更适合 wide
格式的数据,在这种格式中,您可以将国家/地区作为列名,将时间作为索引,并且数据框中单个类别的值。
如果没有适当的数据样本,很难 100% 确定您的问题是什么。但是在我看来,您的数据结构与 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
所以我会根据这个提供一个答案,你可以尝试从那里整理出来。当然,除非您愿意分享完整的数据示例和代码片段。
剧情:
完整代码:
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()
此代码段的作用是将源子集划分为每个独特的类别,例如:
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')
旋转该数据集以获得:
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()
将该数据添加到绘图中。
我正在尝试使用 plotly 为单个图形中的多个数据框绘制折线图。 我的代码是:
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 循环最后一次迭代的折线图。
当前输出:
期望的输出:
请帮我写代码!
将 plotly.express
与 px.line()
结合使用,只要您的数据集是 long
格式。您可能会混淆这种使用 for loop
和 fig.add_figure()
的方法,这可以说更适合 wide
格式的数据,在这种格式中,您可以将国家/地区作为列名,将时间作为索引,并且数据框中单个类别的值。
如果没有适当的数据样本,很难 100% 确定您的问题是什么。但是在我看来,您的数据结构与 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
所以我会根据这个提供一个答案,你可以尝试从那里整理出来。当然,除非您愿意分享完整的数据示例和代码片段。
剧情:
完整代码:
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()
此代码段的作用是将源子集划分为每个独特的类别,例如:
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')
旋转该数据集以获得:
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()
将该数据添加到绘图中。