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 on secondary Y-Axis with Python Plotly?
Plotly is an open-source, interactive, and browser-based charting library for Python. Python users can use Plotly to generate different types of charts including scientific charts, 3D graphs, statistical charts, financial charts, etc.
In this tutorial, we will show how you can use Plotly to plot data on the secondary Y-Axis. Here we will use the plotly.graph_objects module to generate figures. It contains a lot of methods to customize the charts and render them into HTML format. We will plot two bar charts using the add_trace() method and then use the update_layout() method to set a property with dict arguments.
Step-by-Step Implementation
Step 1: Import Required Module
Import the plotly.graph_objects module and alias as go ?
import plotly.graph_objects as go
Step 2: Create a Figure
Create a figure using the Figure() method ?
import plotly.graph_objects as go
fig = go.Figure()
print("Figure created successfully")
Figure created successfully
Step 3: Add Bar Charts with Different Y-Axes
Create two bar charts using the add_trace() method. The first chart uses the primary Y-axis (y1) and the second uses the secondary Y-axis (y2) ?
import plotly.graph_objects as go
fig = go.Figure()
# Add first bar chart (primary Y-axis)
fig.add_trace(go.Bar(
x=[5, 6, 7],
y=[1, 2, 3],
name="Primary Axis",
yaxis='y'
))
# Add second bar chart (secondary Y-axis)
fig.add_trace(go.Bar(
x=[1, 2, 3],
y=[100, 200, 300],
name="Secondary Axis",
yaxis="y2"
))
print("Bar charts added successfully")
Bar charts added successfully
Step 4: Configure the Axes
Create axis objects for the first and second Y-axis using update_layout() ?
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Bar(
x=[5, 6, 7],
y=[1, 2, 3],
name="Primary Axis",
yaxis='y'
))
fig.add_trace(go.Bar(
x=[1, 2, 3],
y=[100, 200, 300],
name="Secondary Axis",
yaxis="y2"
))
# Configure the axes
fig.update_layout(
# Primary Y-axis configuration
yaxis=dict(
title="Primary Y-axis",
titlefont=dict(color="blue"),
tickfont=dict(color="blue")
),
# Secondary Y-axis configuration
yaxis2=dict(
title="Secondary Y-axis",
overlaying="y",
side="right",
titlefont=dict(color="red"),
tickfont=dict(color="red")
)
)
print("Axes configured successfully")
Axes configured successfully
Complete Example
Here is the complete code to plot on secondary Y-axis ?
import plotly.graph_objects as go
# Create figure
fig = go.Figure()
# Add first bar chart (primary Y-axis)
fig.add_trace(go.Bar(
x=['A', 'B', 'C'],
y=[1, 2, 3],
name="Sales (Units)",
yaxis='y',
marker_color='lightblue'
))
# Add second bar chart (secondary Y-axis)
fig.add_trace(go.Bar(
x=['A', 'B', 'C'],
y=[100, 200, 300],
name="Revenue ($)",
yaxis="y2",
marker_color='lightcoral'
))
# Configure layout with dual Y-axes
fig.update_layout(
title="Dual Y-Axis Bar Chart",
# Primary Y-axis (left side)
yaxis=dict(
title="Units Sold",
titlefont=dict(color="blue"),
tickfont=dict(color="blue")
),
# Secondary Y-axis (right side)
yaxis2=dict(
title="Revenue ($)",
overlaying="y",
side="right",
titlefont=dict(color="red"),
tickfont=dict(color="red")
),
# X-axis configuration
xaxis=dict(title="Products"),
# Layout styling
width=700,
height=400,
showlegend=True
)
# Display the chart
fig.show()
Key Parameters
| Parameter | Description | Usage |
|---|---|---|
yaxis |
References primary Y-axis | Default left-side axis |
yaxis2 |
References secondary Y-axis | Right-side axis with overlaying="y"
|
overlaying |
Overlays secondary axis on primary | Always set to "y" for Y-axis |
side |
Position of secondary axis |
"right" or "left"
|
Conclusion
Secondary Y-axes in Plotly allow you to display datasets with different scales on the same chart. Use yaxis2 with overlaying="y" and side="right" to create the secondary axis, making it perfect for comparing related metrics like units and revenue.
