3

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 Answer 1

8

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.