How to plot multiple lines on the same y-axis using plotly express?

Is there a solution to this now?

This is very helpful! It would be great to include a sample line chart with multiple y values on the line charts documentation page. Feel free to use this one, for instance:

df = pd.DataFrame(
    {'Date':['2011-5-10', '2011-5-11', '2011-5-12', '2011-5-13', '2011-5-14',
    '2011-5-15', '2011-5-16', '2011-5-17', '2011-5-18', '2011-5-19'], 
    'Visitors':[532, 510, 217, 554, 293, 442, 510, 632, 440, 799]})
df['3 Day Moving Avg'] = np.round(df['Visitors'].rolling(3).mean(), 2)
px.line(df, x = 'Date', y = ['Visitors', '3 Day Moving Avg'])

Here’s a static screenshot of the output:

2 Likes

You don’t need to convert the DataFrame index into a column to use it as the x-axis in Plotly Express. You can directly pass the index using x=df.index. To plot multiple columns on the same y-axis, Plotly Express allows you to pass a list of columns to the y argument, and it will automatically create multiple line traces sharing the same x-axis.

Example :

import plotly.express as px
import pandas as pd
  
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
  
fig = px.line(
    df,
    x="Date",
    y=["AAPL.High", "AAPL.Low", "AAPL.Open"])
  
fig.show()

Check out how we used Plotly Studio to build a sample app with two columns on the y-axis.