Last modified: Jun 02, 2026
Python Plotly Multiple Line Chart Guide
Multiple line charts are a powerful way to compare trends. They show how different variables change over time. Plotly makes this easy with clean and interactive plots.
This guide will teach you how to create multiple line charts in Python. We will use plotly.graph_objects and plotly.express. You will learn step-by-step with code examples.
Why Use Multiple Line Charts?
Multiple line charts help you see patterns side by side. For example, compare sales of different products. Or track temperature in multiple cities.
They are great for time series data. You can spot correlations and differences quickly. Plotly adds interactivity like hover and zoom.
Setup Your Environment
First, install Plotly. Open your terminal or command prompt. Run this command:
pip install plotly
Now you are ready. We will start with a simple example.
Basic Multiple Line Chart with Plotly Express
Plotly Express is the high-level API. It is simple and fast. Let's create a chart with three lines.
import plotly.express as px
import pandas as pd
# Sample data
data = {
'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
'Product_A': [10, 15, 13, 17, 20],
'Product_B': [8, 12, 14, 11, 16],
'Product_C': [5, 7, 9, 10, 12]
}
df = pd.DataFrame(data)
# Create multiple line chart
fig = px.line(df, x='Month', y=['Product_A', 'Product_B', 'Product_C'],
title='Monthly Sales Comparison',
labels={'value': 'Sales', 'variable': 'Product'})
fig.show()
Important: The y parameter accepts a list of column names. Each column becomes a separate line.
The chart will show three lines with different colors. Hover over any point to see exact values.
Using Graph Objects for More Control
For advanced customization, use plotly.graph_objects. This gives you full control over every element.
Check out our Python Plotly Graph Objects Visualization Guide for a deeper dive.
Here is the same chart using graph objects:
import plotly.graph_objects as go
fig = go.Figure()
# Add traces (lines)
fig.add_trace(go.Scatter(x=df['Month'], y=df['Product_A'],
mode='lines+markers', name='Product A'))
fig.add_trace(go.Scatter(x=df['Month'], y=df['Product_B'],
mode='lines+markers', name='Product B'))
fig.add_trace(go.Scatter(x=df['Month'], y=df['Product_C'],
mode='lines+markers', name='Product C'))
# Update layout
fig.update_layout(title='Monthly Sales Comparison',
xaxis_title='Month',
yaxis_title='Sales')
fig.show()
Notice how we add each line one by one. The name parameter sets the legend label.
Customize Line Styles
You can change colors, dash patterns, and widths. This makes your chart more readable.
fig = go.Figure()
fig.add_trace(go.Scatter(x=df['Month'], y=df['Product_A'],
mode='lines', line=dict(color='blue', width=3),
name='Product A'))
fig.add_trace(go.Scatter(x=df['Month'], y=df['Product_B'],
mode='lines', line=dict(color='red', dash='dash', width=2),
name='Product B'))
fig.add_trace(go.Scatter(x=df['Month'], y=df['Product_C'],
mode='lines', line=dict(color='green', dash='dot', width=2),
name='Product C'))
fig.update_layout(title='Styled Multiple Line Chart')
fig.show()
Tip: Use dashed lines for secondary data. Solid lines for primary data.
Add Markers and Annotations
Markers highlight individual data points. Annotations add text notes.
fig = go.Figure()
fig.add_trace(go.Scatter(x=df['Month'], y=df['Product_A'],
mode='lines+markers', marker=dict(size=8, symbol='circle'),
name='Product A'))
# Add annotation
fig.add_annotation(x='Mar', y=13,
text="Peak in March",
showarrow=True,
arrowhead=1)
fig.update_layout(title='Chart with Markers and Annotations')
fig.show()
Annotations help explain unusual points. They make your chart more informative.
Working with Real Data
Let's use real-world data. We will plot temperature for three cities over a year.
import pandas as pd
import plotly.graph_objects as go
# Simulated temperature data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
city_a = [2, 4, 8, 12, 18, 22, 25, 24, 20, 14, 8, 3]
city_b = [10, 12, 15, 20, 25, 30, 33, 32, 28, 22, 16, 11]
city_c = [-5, -2, 3, 10, 16, 20, 22, 21, 17, 10, 3, -2]
fig = go.Figure()
fig.add_trace(go.Scatter(x=months, y=city_a, mode='lines+markers', name='New York'))
fig.add_trace(go.Scatter(x=months, y=city_b, mode='lines+markers', name='Los Angeles'))
fig.add_trace(go.Scatter(x=months, y=city_c, mode='lines+markers', name='Chicago'))
fig.update_layout(title='Average Monthly Temperature',
xaxis_title='Month',
yaxis_title='Temperature (°C)')
fig.show()
This chart clearly shows climate differences. Los Angeles is warmer year-round. Chicago has cold winters.
Handling Multiple Axes
Sometimes variables have different scales. Use dual y-axes to compare them.
fig = go.Figure()
# First line uses left y-axis
fig.add_trace(go.Scatter(x=months, y=city_a, name='Temperature',
yaxis='y1', line=dict(color='red')))
# Second line uses right y-axis
fig.add_trace(go.Scatter(x=months, y=[5, 6, 8, 10, 12, 14, 15, 14, 12, 10, 7, 5],
name='Rainfall (mm)',
yaxis='y2', line=dict(color='blue', dash='dash')))
# Create axis objects
fig.update_layout(
title='Temperature and Rainfall',
xaxis=dict(title='Month'),
yaxis=dict(title='Temperature (°C)', side='left'),
yaxis2=dict(title='Rainfall (mm)', side='right', overlaying='y', anchor='x')
)
fig.show()
Note: Use overlaying='y' to stack the axes. This creates a dual-axis chart.
Save and Export Charts
Plotly charts can be saved as HTML or PNG. Use the write_html or write_image methods.
# Save as interactive HTML
fig.write_html('multiple_line_chart.html')
# Save as static PNG (requires kaleido)
# fig.write_image('chart.png')
HTML files preserve interactivity. PNG files are good for reports.
Common Mistakes and Tips
Beginners often forget to set the x and y correctly. Always check your data types.
Another mistake is using too many lines. Limit to 5-7 lines for readability.
Use colorblind-friendly palettes. Plotly has built-in themes like plotly_dark or seaborn.
For more advanced techniques, revisit our Python Plotly Graph Objects Visualization Guide for detailed examples.
Conclusion
Multiple line charts are essential for data comparison. Plotly makes them interactive and beautiful.
You learned to create charts with plotly.express and plotly.graph_objects. You can customize styles, add markers, and handle multiple axes.
Practice with your own data. Start with small datasets. Add one feature at a time.
Plotly is a versatile library. Keep exploring its features for better visualizations.