简体   繁体   中英

plot multiple pandas dataframes in one graph python plotly

I want to plot line charts for multiple pandas data frames in one graph using python plotly (set x-axis: Day-shift and y-axis: PRO ).

I tried using for loop but it failed to generate expected output.Code I tried is given below.

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()

Also I want to set legend as df1 to df5

With plotly you can try the following:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM