[英]Plotly line chart from pandas dataframe with multiple lines
This is my input dataframe:这是我的输入 dataframe:
labels percentile_5 percentile_25 median percentile_75 percentile_98
0 0 21 25 27 30 34
1 1 49 52 56 61 71
2 2 34 36 39 43 48
I have tried transform
using the below code:我尝试使用以下代码进行transform
:
t = df.T.reset_index()
t.columns = t.iloc[0]
t = t.iloc[1:]
Output: Output:
labels 0 1 2
1 percentile_5 21 49 34
2 percentile_25 25 52 36
3 median 27 56 39
4 percentile_75 30 61 43
5 percentile_98 34 71 48
Using this I can create line chart with only one line.使用它我可以创建只有一条线的折线图。
fig = px.line(t, x="labels", y=0)
fig.show()
I am trying to plot like this, x-axis = labels, legend = 0,1,2我正在尝试像这样 plot,x 轴 = 标签,图例 = 0,1,2
What are the changes I need to do in dataframe or is there any easy way to plot using the first dataframe?我需要在 dataframe 中做哪些更改,或者是否有任何简单的方法可以使用第一个 dataframe 来 plot?
If I understand correctly you don't have to do any changes.如果我理解正确,您无需进行任何更改。 Just run:赶紧跑:
fig = px.line(df, x='labels', y = df.columns)
This should be what you're looking for since you've specified x=labels
and legend = 0, 1, 2
.这应该是您要查找的内容,因为您已指定x=labels
和legend = 0, 1, 2
。
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'labels': {1: 'percentile_5',
2: 'percentile_25',
3: 'median',
4: 'percentile_75',
5: 'percentile_98'},
'0': {1: 21, 2: 25, 3: 27, 4: 30, 5: 34},
'1': {1: 49, 2: 52, 3: 56, 4: 61, 5: 71},
'2': {1: 34, 2: 36, 3: 39, 4: 43, 5: 48}})
fig = px.line(df, x='labels', y = df.columns)
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.