【问题标题】:plot multiple pandas dataframes in one graph python plotly在一张图中绘制多个熊猫数据框 python plotly
【发布时间】:2020-10-23 12:56:30
【问题描述】:

我想使用 python plotly多个 pandas 数据帧在一个图表中绘制折线图(设置 x 轴:Day-shift和 y 轴:PRO)。

我尝试使用 for 循环,但未能生成预期的输出。我尝试的代码如下。

import plotly.express as px
for frame in [df1, df2, df3, df4, df5]:
    fig = px.line(frame, x='Day-Shift', y='PRO',template="plotly_dark")


fig.show()

我还想将图例设置为 df1 到 df5

【问题讨论】:

    标签: python pandas dataframe linechart plotly-python


    【解决方案1】:

    使用 plotly 您可以尝试以下操作:

    import pandas as pd
    import plotly.graph_objects as go
    from plotly.offline import iplot
    
    # dict for the dataframes and their names
    dfs = {"df1" : df1, "df2": df2, "df3" : df3, "df4" : df4, "df5" : df5}
    
    # plot the data
    fig = go.Figure()
    
    for i in dfs:
        fig = fig.add_trace(go.Scatter(x = dfs[i]["day-shift"],
                                       y = dfs[i]["PRO"], 
                                       name = i))
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 2016-01-07
      • 2019-04-16
      • 2022-01-08
      • 1970-01-01
      • 2018-01-10
      • 2016-05-16
      • 1970-01-01
      • 2021-11-05
      • 2022-01-01
      相关资源
      最近更新 更多