Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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


1 Answers

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()
like image 108
Bibzon Avatar answered Mar 07 '26 16:03

Bibzon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!