我想使用 python plotly (在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
答案 0 :(得分:1)
您可以尝试以下方法:
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()