Python Plotly – 如何添加多个 Y 轴?
在本文中,让我们看看如何在Python中的 Plotly 图表中添加多个不同比例的 y 轴。
目前,Plotly Express 不支持单个图形上的多个 Y 轴。所以,我们将使用 Plotly go。 Plotly 提供了一个名为make_subplots()的函数来绘制具有多个 Y 轴的图表。
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.
首先,从Plotly 包中导入必要的函数,并使用make_subplots()函数中的 specs 参数创建辅助轴,如图所示。绘制具有多个 y 轴的散点图。通过在x和y 轴上添加标题来使图表可读。
示例:添加 2 个 y 轴
Python3
# import graph_objects from plotly package
import plotly.graph_objects as go
# import make_subplots function from plotly.subplots
# to make grid of plots
from plotly.subplots import make_subplots
# use specs parameter in make_subplots function
# to create secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])
# plot a scatter chart by specifying the x and y values
# Use add_trace function to specify secondary_y axes.
fig.add_trace(
go.Scatter(x=[10, 20, 30], y=[400, 500, 600], name="yaxis values"),
secondary_y=False)
# Use add_trace function and specify secondary_y axes = True.
fig.add_trace(
go.Scatter(x=[20, 30, 40], y=[40, 50, 60], name="yaxis2 values"),
secondary_y=True,)
# Adding title text to the figure
fig.update_layout(
title_text="Multiple Y Axis in Plotly"
)
# Naming x-axis
fig.update_xaxes(title_text="X - axis")
# Naming y-axes
fig.update_yaxes(title_text="Main Y - axis ", secondary_y=False)
fig.update_yaxes(title_text="secondary Y - axis ", secondary_y=True)
Python3
# import graph_objects from plotly package
import plotly.graph_objects as go
# import make_subplots function from plotly.subplots
# to make grid of plots
from plotly.subplots import make_subplots
# use specs parameter in make_subplots function
# to create secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])
# plot a bar chart by specifying the x and y values
# Use add_trace function to specify secondary_y axes.
fig.add_trace(
go.Bar(x=[10, 20, 30], y=[40, 50, 60], name="yaxis values"),
secondary_y=False)
# Use add_trace function and specify secondary_y axes = True.
fig.add_trace(
go.Bar(x=[20, 30, 40], y=[400, 500, 600], name="yaxis2 values"),
secondary_y=True,)
# Adding title text to the figure
fig.update_layout(
title_text="Multiple Y Axis in Plotly"
)
# Naming x-axis
fig.update_xaxes(title_text="X - axis")
# Naming y-axes
fig.update_yaxes(title_text="Main Y - axis ", secondary_y=False)
fig.update_yaxes(title_text="secondary Y - axis ", secondary_y=True)
Python3
# import the graph_objects function from
# plotly package
import plotly.graph_objects as go
# initialize a Figure object and store it in
# a variable fig
fig = go.Figure()
# add x and y values for the 1st scatter
# plot and name the yaxis as yaxis1 values
fig.add_trace(go.Scatter(
x=[10, 12, 13],
y=[41, 58, 60],
name="yaxis1 values"
))
# add x and y values for the 2nd scatter
# plot and name the yaxis as yaxis2 values
fig.add_trace(go.Scatter(
x=[12, 13, 14],
y=[401, 501, 610],
name="yaxis2 values",
yaxis="y2"
))
# add x and y values for the 3rd scatter
# plot and name the yaxis as yaxis3 values
fig.add_trace(go.Scatter(
x=[14, 15, 16],
y=[42000, 53000, 65000],
name="yaxis3 values",
yaxis="y3"
))
# add x and y values for the 4th scatter plot
# and name the yaxis as yaxis4 values
fig.add_trace(go.Scatter(
x=[15, 16, 17],
y=[2000, 5000, 7000],
name="yaxis4 values",
yaxis="y4"
))
# Create axis objects
fig.update_layout(
# split the x-axis to fraction of plots in
# proportions
xaxis=dict(
domain=[0.3, 0.7]
),
# pass the y-axis title, titlefont, color
# and tickfont as a dictionary and store
# it an variable yaxis
yaxis=dict(
title="yaxis 1",
titlefont=dict(
color="#0000ff"
),
tickfont=dict(
color="#0000ff"
)
),
# pass the y-axis 2 title, titlefont, color and
# tickfont as a dictionary and store it an
# variable yaxis 2
yaxis2=dict(
title="yaxis 2",
titlefont=dict(
color="#FF0000"
),
tickfont=dict(
color="#FF0000"
),
anchor="free", # specifying x - axis has to be the fixed
overlaying="y", # specifyinfg y - axis has to be separated
side="left", # specifying the side the axis should be present
position=0.2 # specifying the position of the axis
),
# pass the y-axis 3 title, titlefont, color and
# tickfont as a dictionary and store it an
# variable yaxis 3
yaxis3=dict(
title="yaxis 3",
titlefont=dict(
color="#006400"
),
tickfont=dict(
color="#006400"
),
anchor="x", # specifying x - axis has to be the fixed
overlaying="y", # specifyinfg y - axis has to be separated
side="right" # specifying the side the axis should be present
),
# pass the y-axis 4 title, titlefont, color and
# tickfont as a dictionary and store it an
# variable yaxis 4
yaxis4=dict(
title="yaxis 4",
titlefont=dict(
color="#8f00ff"
),
tickfont=dict(
color="#8f00ff"
),
anchor="free", # specifying x - axis has to be the fixed
overlaying="y", # specifyinfg y - axis has to be separated
side="right", # specifying the side the axis should be present
position=0.8 # specifying the position of the axis
)
)
# Update layout of the plot namely title_text, width
# and place it in the center using title_x parameter
# as shown
fig.update_layout(
title_text="4 y-axes scatter plot in plotly",
width=1000,
title_x=0.5
)
输出:
示例:添加 2 个 y 轴
Python3
# import graph_objects from plotly package
import plotly.graph_objects as go
# import make_subplots function from plotly.subplots
# to make grid of plots
from plotly.subplots import make_subplots
# use specs parameter in make_subplots function
# to create secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])
# plot a bar chart by specifying the x and y values
# Use add_trace function to specify secondary_y axes.
fig.add_trace(
go.Bar(x=[10, 20, 30], y=[40, 50, 60], name="yaxis values"),
secondary_y=False)
# Use add_trace function and specify secondary_y axes = True.
fig.add_trace(
go.Bar(x=[20, 30, 40], y=[400, 500, 600], name="yaxis2 values"),
secondary_y=True,)
# Adding title text to the figure
fig.update_layout(
title_text="Multiple Y Axis in Plotly"
)
# Naming x-axis
fig.update_xaxes(title_text="X - axis")
# Naming y-axes
fig.update_yaxes(title_text="Main Y - axis ", secondary_y=False)
fig.update_yaxes(title_text="secondary Y - axis ", secondary_y=True)
输出:
绘制具有多个 Y 轴的图表
现在,让我们看看如何绘制具有2 个以上 Y 轴或多个 Y 轴的散点图。过程同上,在图形布局部分进行了更改,使图表在视觉上更令人愉悦。
从Plotly 包中导入必要的函数。如图所示,使用make_subplots函数中的 specs 参数创建辅助轴。绘制具有多个 y 轴的散点图。
示例:添加多个 y 轴
Python3
# import the graph_objects function from
# plotly package
import plotly.graph_objects as go
# initialize a Figure object and store it in
# a variable fig
fig = go.Figure()
# add x and y values for the 1st scatter
# plot and name the yaxis as yaxis1 values
fig.add_trace(go.Scatter(
x=[10, 12, 13],
y=[41, 58, 60],
name="yaxis1 values"
))
# add x and y values for the 2nd scatter
# plot and name the yaxis as yaxis2 values
fig.add_trace(go.Scatter(
x=[12, 13, 14],
y=[401, 501, 610],
name="yaxis2 values",
yaxis="y2"
))
# add x and y values for the 3rd scatter
# plot and name the yaxis as yaxis3 values
fig.add_trace(go.Scatter(
x=[14, 15, 16],
y=[42000, 53000, 65000],
name="yaxis3 values",
yaxis="y3"
))
# add x and y values for the 4th scatter plot
# and name the yaxis as yaxis4 values
fig.add_trace(go.Scatter(
x=[15, 16, 17],
y=[2000, 5000, 7000],
name="yaxis4 values",
yaxis="y4"
))
# Create axis objects
fig.update_layout(
# split the x-axis to fraction of plots in
# proportions
xaxis=dict(
domain=[0.3, 0.7]
),
# pass the y-axis title, titlefont, color
# and tickfont as a dictionary and store
# it an variable yaxis
yaxis=dict(
title="yaxis 1",
titlefont=dict(
color="#0000ff"
),
tickfont=dict(
color="#0000ff"
)
),
# pass the y-axis 2 title, titlefont, color and
# tickfont as a dictionary and store it an
# variable yaxis 2
yaxis2=dict(
title="yaxis 2",
titlefont=dict(
color="#FF0000"
),
tickfont=dict(
color="#FF0000"
),
anchor="free", # specifying x - axis has to be the fixed
overlaying="y", # specifyinfg y - axis has to be separated
side="left", # specifying the side the axis should be present
position=0.2 # specifying the position of the axis
),
# pass the y-axis 3 title, titlefont, color and
# tickfont as a dictionary and store it an
# variable yaxis 3
yaxis3=dict(
title="yaxis 3",
titlefont=dict(
color="#006400"
),
tickfont=dict(
color="#006400"
),
anchor="x", # specifying x - axis has to be the fixed
overlaying="y", # specifyinfg y - axis has to be separated
side="right" # specifying the side the axis should be present
),
# pass the y-axis 4 title, titlefont, color and
# tickfont as a dictionary and store it an
# variable yaxis 4
yaxis4=dict(
title="yaxis 4",
titlefont=dict(
color="#8f00ff"
),
tickfont=dict(
color="#8f00ff"
),
anchor="free", # specifying x - axis has to be the fixed
overlaying="y", # specifyinfg y - axis has to be separated
side="right", # specifying the side the axis should be present
position=0.8 # specifying the position of the axis
)
)
# Update layout of the plot namely title_text, width
# and place it in the center using title_x parameter
# as shown
fig.update_layout(
title_text="4 y-axes scatter plot in plotly",
width=1000,
title_x=0.5
)
输出: