【问题标题】:Plotly line chart from pandas dataframe with multiple lines用多条线从熊猫数据框中绘制折线图
【发布时间】:2021-03-17 07:40:09
【问题描述】:

这是我的输入数据框:

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

我已尝试使用以下代码transform

t = df.T.reset_index()
t.columns = t.iloc[0]
t = t.iloc[1:]

输出:

     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

使用它我可以创建只有一条线的折线图。

fig = px.line(t, x="labels", y=0)
fig.show()

我正在尝试这样绘制,x 轴 = 标签,图例 = 0,1,2

我需要在数据框或 有没有使用第一个数据框绘制的简单方法?

【问题讨论】:

    标签: python python-3.x pandas dataframe plotly


    【解决方案1】:

    如果我理解正确,您无需进行任何更改。运行:

    fig = px.line(df, x='labels', y = df.columns)
    

    应该是你正在寻找的,因为你已经指定了x=labelslegend = 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()
    

    【讨论】:

      猜你喜欢
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 2016-03-23
      • 2020-03-18
      • 2019-10-27
      • 2018-10-15
      相关资源
      最近更新 更多