简体   繁体   English

Plotly 折线图来自 pandas dataframe 带多条线

[英]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=labelslegend = 0, 1, 2

Complete code完整代码

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.

相关问题 Pandas 数据框绘制两条线的折线图 - Pandas dataframe Plotly line chart with two lines Plotly:如何在来自同一 Pandas 数据帧的不同列的一个绘图图表中绘制多条线? - Plotly: How to plot multiple lines in one plotly chart from different columns from the same pandas dataframe? 来自熊猫数据框的 plotly 折线图 - plotly Line chart from pandas dataframe 如何 plot 多行在一个 plotly 图表中来自同一列的同一列 pandas Z6A8064B5DF47945550705553? - How to plot multiple lines in one plotly chart from same column from the same pandas dataframe? Plot 线图来自 Pandas dataframe(多线) - Plot line graph from Pandas dataframe (with multiple lines) 使用 plotly 按结果从 pandas 组随时间将多条线绘制到同一个图表中 - Plot multiple lines into the same chart over time from pandas group by result using plotly Plotly:如何从 pandas dataframe 创建垂直堆叠条形图? - Plotly: How to create a vertically stacked bar chart from a pandas dataframe? 使用Pandas从熊猫数据框中绘制分组的条形图 - Plotting a grouped bar chart using Plotly from a pandas dataframe 来自 dataframe 的多条折线图,带有循环样本 - Multiple lines chart from dataframe with looping samples pandas - 将多行从一个 dataframe 连接到另一个 dataframe 中的一行 - pandas - joining multiple lines from one dataframe to just one line in another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM