绘制两个 Y 轴:如何将一个数据集显示为折线图,将第二个数据集显示为散点图
Plotly two Y axes: how to show one dataset as a linechart and the second one as a scatter plot
我查看了文档,但没有找到问题的答案。
此代码来自 Plotly (link here) 并允许从两个不同的数据集创建具有两个 Y 轴的图:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])
# Add traces
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis data"),
secondary_y=False,
)
fig.add_trace(
go.Scatter(x=[2, 3, 4], y=[4, 5, 6], name="yaxis2 data"),
secondary_y=True,
)
# Add figure title
fig.update_layout(
title_text="Double Y Axis Example"
)
# Set x-axis title
fig.update_xaxes(title_text="xaxis title")
# Set y-axes titles
fig.update_yaxes(title_text="<b>primary</b> yaxis title", secondary_y=False)
fig.update_yaxes(title_text="<b>secondary</b> yaxis title", secondary_y=True)
fig.show()
现在。我希望第二个 Y 轴上显示的数据是散点图而不是直线。有办法吗?
提前致谢。
您可以将 mode
设置为 "markers"
而不是默认值(即 "lines+markers"
):
fig.add_trace(
go.Scatter(x=[2, 3, 4], y=[4, 5, 6], name="yaxis2 data", mode="markers"),
secondary_y=True,
)
我查看了文档,但没有找到问题的答案。
此代码来自 Plotly (link here) 并允许从两个不同的数据集创建具有两个 Y 轴的图:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])
# Add traces
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis data"),
secondary_y=False,
)
fig.add_trace(
go.Scatter(x=[2, 3, 4], y=[4, 5, 6], name="yaxis2 data"),
secondary_y=True,
)
# Add figure title
fig.update_layout(
title_text="Double Y Axis Example"
)
# Set x-axis title
fig.update_xaxes(title_text="xaxis title")
# Set y-axes titles
fig.update_yaxes(title_text="<b>primary</b> yaxis title", secondary_y=False)
fig.update_yaxes(title_text="<b>secondary</b> yaxis title", secondary_y=True)
fig.show()
现在。我希望第二个 Y 轴上显示的数据是散点图而不是直线。有办法吗?
提前致谢。
您可以将 mode
设置为 "markers"
而不是默认值(即 "lines+markers"
):
fig.add_trace(
go.Scatter(x=[2, 3, 4], y=[4, 5, 6], name="yaxis2 data", mode="markers"),
secondary_y=True,
)