Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to plot multiple lines on the same Y-axis in Python Plotly?
Plotly is an open-source plotting library in Python that creates interactive web-based visualizations. In this tutorial, we will show how to plot multiple lines on the same Y-axis using plotly.express and pandas.
When working with time-series data or comparing multiple metrics, plotting multiple lines on the same chart helps visualize trends and relationships between different datasets.
Method 1: Using add_scatter()
First, create a line plot and then add another line using add_scatter() method ?
import plotly.express as px
import pandas as pd
# Create dataset
data = {
'year': [2015, 2016, 2017, 2018, 2019],
'lifeexp': [75, 74, 72, 70, 69],
'state': ['kerala', 'punjab', 'karnataka', 'andhra', 'odisha'],
'ratio': [74, 73.9, 71.5, 69.8, 69]
}
df = pd.DataFrame(data)
# Create line plot
fig = px.line(df, x='year', y='lifeexp', title='Multiple Lines on Same Y-axis')
# Add second line using scatter with line mode
fig.add_scatter(x=df['year'], y=df['ratio'], mode='lines', name='Ratio')
# Show the plot
fig.show()
Method 2: Using Multiple Columns with Melt
Transform the DataFrame to have multiple lines in a single plot using pd.melt() ?
import plotly.express as px
import pandas as pd
# Create dataset
data = {
'year': [2015, 2016, 2017, 2018, 2019],
'lifeexp': [75, 74, 72, 70, 69],
'ratio': [74, 73.9, 71.5, 69.8, 69]
}
df = pd.DataFrame(data)
# Melt the dataframe to create multiple lines
df_melted = pd.melt(df, id_vars=['year'], value_vars=['lifeexp', 'ratio'],
var_name='metric', value_name='value')
# Create line plot with color parameter
fig = px.line(df_melted, x='year', y='value', color='metric',
title='Multiple Lines Using Melt')
fig.show()
Method 3: Using Graph Objects
For more control over individual lines, use plotly.graph_objects ?
import plotly.graph_objects as go
import pandas as pd
# Create dataset
data = {
'year': [2015, 2016, 2017, 2018, 2019],
'lifeexp': [75, 74, 72, 70, 69],
'ratio': [74, 73.9, 71.5, 69.8, 69]
}
df = pd.DataFrame(data)
# Create figure
fig = go.Figure()
# Add first line
fig.add_trace(go.Scatter(x=df['year'], y=df['lifeexp'],
mode='lines', name='Life Expectancy'))
# Add second line
fig.add_trace(go.Scatter(x=df['year'], y=df['ratio'],
mode='lines', name='Ratio'))
# Update layout
fig.update_layout(title='Multiple Lines with Graph Objects',
xaxis_title='Year',
yaxis_title='Values')
fig.show()
Comparison
| Method | Best For | Pros | Cons |
|---|---|---|---|
add_scatter() |
Simple additions | Quick and easy | Limited customization |
melt() |
Multiple similar metrics | Clean data structure | Requires data transformation |
graph_objects |
Full customization | Complete control | More verbose code |
Key Points
Use name parameter to add legends for multiple lines
The color parameter in px.line() automatically creates multiple lines
mode='lines' ensures only lines are drawn without markers
All methods share the same Y-axis scale automatically
Conclusion
Use add_scatter() for simple multi-line plots, pd.melt() with px.line() for clean data structure, or graph_objects for full customization control. All methods effectively plot multiple lines on the same Y-axis.
