Python Plotly – How to add multiple Y-axes?
In this article let’s see how to add multiple y-axes of different scales in Plotly charts in Python.
Currently, Plotly Express does not support multiple Y axes on a single figure. So, we shall use Plotly go. Plotly provides a function called make_subplots() to plot charts with multiple Y – axes.
Syntax:
plotly.subplots.make_subplots(rows=1, cols=1, specs=None)
Parameter:
- rows: A number of rows in the subplot grid. (rows > 0). default 1
- cols: A number of columns in the subplot grid. (cols> 0). default 1
- specs: (list of lists of dict or None (default None)). Per subplot specifications of subplot type, row/column spanning, and spacing.
First, import the necessary functions from the Plotly package and create the secondary axes using the specs parameter in the make_subplots() function as shown. Plot a scatter plot with multiple y-axes. Make the chart readable by adding titles to the x and y axes.
Example: Adding 2 y-axis
Python3
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(specs = [[{ "secondary_y" : True }]])
fig.add_trace(
go.Scatter(x = [ 10 , 20 , 30 ], y = [ 400 , 500 , 600 ], name = "yaxis values" ),
secondary_y = False )
fig.add_trace(
go.Scatter(x = [ 20 , 30 , 40 ], y = [ 40 , 50 , 60 ], name = "yaxis2 values" ),
secondary_y = True ,)
fig.update_layout(
title_text = "Multiple Y Axis in Plotly"
)
fig.update_xaxes(title_text = "X - axis" )
fig.update_yaxes(title_text = "<b>Main</b> Y - axis " , secondary_y = False )
fig.update_yaxes(title_text = "<b>secondary</b> Y - axis " , secondary_y = True )
|
Output:
Plotly scatter plot with multiple y-axes
Example: Adding 2 y-axis
Python3
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(specs = [[{ "secondary_y" : True }]])
fig.add_trace(
go.Bar(x = [ 10 , 20 , 30 ], y = [ 40 , 50 , 60 ], name = "yaxis values" ),
secondary_y = False )
fig.add_trace(
go.Bar(x = [ 20 , 30 , 40 ], y = [ 400 , 500 , 600 ], name = "yaxis2 values" ),
secondary_y = True ,)
fig.update_layout(
title_text = "Multiple Y Axis in Plotly"
)
fig.update_xaxes(title_text = "X - axis" )
fig.update_yaxes(title_text = "<b>Main</b> Y - axis " , secondary_y = False )
fig.update_yaxes(title_text = "<b>secondary</b> Y - axis " , secondary_y = True )
|
Output:
Plotly bar chart with multiple y axes
Plotly chart with multiple Y – axes
Now, let us look at how to plot a scatter chart with more than 2 Y-axes or multiple Y-axis. The procedure is the same as above, the change comes in the figure layout part to make the chart more visually pleasing.
Import the necessary functions from the Plotly package. Create the secondary axes using the specs parameter in the make_subplots function as shown. Plot a scatter plot with multiple y-axes.
Example: Adding more than multiple y-axis
Python3
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(
x = [ 10 , 12 , 13 ],
y = [ 41 , 58 , 60 ],
name = "yaxis1 values"
))
fig.add_trace(go.Scatter(
x = [ 12 , 13 , 14 ],
y = [ 401 , 501 , 610 ],
name = "yaxis2 values" ,
yaxis = "y2"
))
fig.add_trace(go.Scatter(
x = [ 14 , 15 , 16 ],
y = [ 42000 , 53000 , 65000 ],
name = "yaxis3 values" ,
yaxis = "y3"
))
fig.add_trace(go.Scatter(
x = [ 15 , 16 , 17 ],
y = [ 2000 , 5000 , 7000 ],
name = "yaxis4 values" ,
yaxis = "y4"
))
fig.update_layout(
xaxis = dict (
domain = [ 0.3 , 0.7 ]
),
yaxis = dict (
title = "yaxis 1" ,
titlefont = dict (
color = "#0000ff"
),
tickfont = dict (
color = "#0000ff"
)
),
yaxis2 = dict (
title = "yaxis 2" ,
titlefont = dict (
color = "#FF0000"
),
tickfont = dict (
color = "#FF0000"
),
anchor = "free" ,
overlaying = "y" ,
side = "left" ,
position = 0.2
),
yaxis3 = dict (
title = "yaxis 3" ,
titlefont = dict (
color = "#006400"
),
tickfont = dict (
color = "#006400"
),
anchor = "x" ,
overlaying = "y" ,
side = "right"
),
yaxis4 = dict (
title = "yaxis 4" ,
titlefont = dict (
color = "#8f00ff"
),
tickfont = dict (
color = "#8f00ff"
),
anchor = "free" ,
overlaying = "y" ,
side = "right" ,
position = 0.8
)
)
fig.update_layout(
title_text = "4 y-axes scatter plot in plotly" ,
width = 1000 ,
title_x = 0.5
)
|
Output:
Plotly scatter plot with multiple y – axes
Last Updated :
12 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...