【问题标题】:Plotly: How to plot multiple lines in one plotly chart from different columns from the same pandas dataframe?Plotly:如何在一个 plotly 图表中从同一个 pandas 数据框的不同列中绘制多条线?
【发布时间】:2021-01-26 15:55:08
【问题描述】:

我有下表作为熊猫:

>>>date          hour      plant1   plant2  plant3  plant4    ...
0 2019-06-23    07:00:00   251.2     232.7   145.1   176.7
1 2019-06-23    07:02:00   123.4     173.1   121.5   180.4
2 2019-06-23    07:04:00   240.1     162.7   140.1   199.5
3 2019-06-23    07:06:00   224.8     196.5   134.1   200.5
4 2019-06-23    07:08:00   124.3     185.4   132.3   190.1
...

我想以交互方式绘制每个植物(每列)以创建包含所有植物列的线图。

只用 plotly 绘制一条线对我有用:

import plotly.express as px

fig = px.line(df, x=df.iloc[:,2], y=df.iloc[:,3])
fig.show()

但是当我尝试使用 iloc 绘制所有列并像这样放置所有列时它失败了:

fig = px.line(df, x=df.iloc[:,2], y=df.iloc[:,3:])

ValueError:所有参数的长度应该相同。的长度 列参数df[wide_variable_0] 是 2814,而 先前处理的参数 ['x'] 是 201

我知道 for plotly 不理解我的 iloc 输入来单独绘制每一列。

我如何告诉它将每列绘制为单独的行(例如,像这样,但使用我的数据和每列的行,因此我们将使用列名而不是国家/地区):

*此示例来自 plotly 手册 (https://plotly.com/python/line-charts/)

我的最终目标:将每列绘制为每个植物列的线

编辑:我也尝试过这里描述的熊猫,但是由于某种原因,当我这样尝试时,我得到了错误:

dfs['2019-06-23'].iloc[:,2:].plot(kind='line')
 >>>ImportError: matplotlib is required for plotting when the default backend "matplotlib" is selected.

但是当我“改变顺序”时:

plt.plot(df.iloc[:,2:])

它可以工作,但不是交互式的。

【问题讨论】:

    标签: python pandas matplotlib plotly plotly-python


    【解决方案1】:

    你可以简单地使用define the column nameslist(df.columns) 来绘制你想在list(df.columns) 中绘制的to_plot = [v for v in list(df.columns) if v.startswith('plant')],然后使用fig = px.line(df, x=df.index, y=to_plot) 得到:

    完整代码:

    import pandas as pd
    import plotly.express as px
    
    df = pd.DataFrame({'date': {0: '2019-06-23',
                                  1: '2019-06-23',
                                  2: '2019-06-23',
                                  3: '2019-06-23',
                                  4: '2019-06-23'},
                                 'hour': {0: '07:00:00',
                                  1: '07:02:00',
                                  2: '07:04:00',
                                  3: '07:06:00',
                                  4: '07:08:00'},
                                 'plant1': {0: 251.2, 1: 123.4, 2: 240.1, 3: 224.8, 4: 124.3},
                                 'plant2': {0: 232.7, 1: 173.1, 2: 162.7, 3: 196.5, 4: 185.4},
                                 'plant3': {0: 145.1, 1: 121.5, 2: 140.1, 3: 134.1, 4: 132.3},
                                 'plant4': {0: 176.7, 1: 180.4, 2: 199.5, 3: 200.5, 4: 190.1}})
    
    df['ix'] = df['date']+' ' +df['hour']
    df['ix'] = pd.to_datetime(df['ix'])
    
    to_plot = [v for v in list(df.columns) if v.startswith('plant')]
    
    fig = px.line(df, x=df.index, y=to_plot)
    fig.show()
    

    【讨论】:

    • 你成就了我的一天。我找了好几个小时。我发现 Plotly 文档有些不足。你有学习 Plotly Express 的好资源吗?
    【解决方案2】:

    您能提供一部分数据吗? 我不知道你到底用什么作为x。 df.iloc[:,2] 看起来像plant1

    一般来说,新版本的 plotly 可能需要多个 y,旧版本可能不会;如果更新包仍然不起作用,请像这样合并数据框:

    list =  # all the lines you want to draw, eg ['plant1','plant2']
    
    df = pd.melt(df,
                id_vars=["date", "hour"],
                value_vars= list ,
                var_name="plant_number",
                value_name="y")
    
    fig = px.line(df, x= "date", y="y" ,color = "plant_number")
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 2021-10-26
      • 2021-07-25
      • 2021-06-25
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多