简体   繁体   English

绘制两个 Y 轴:如何将一个数据集显示为折线图,将第二个数据集显示为散点图

[英]Plotly two Y axes: how to show one dataset as a linechart and the second one as a scatter plot

I have checked the documentation, but I have not found an answer to my question.我已经检查了文档,但我没有找到我的问题的答案。

This code comes from Plotly (link here ) and allows to create a plot with two Y axis from two different data sets:此代码来自 Plotly(链接此处),并允许从两个不同的数据集创建具有两个 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()

Now.现在。 I want the data displayed in the second Y axis to be a scatter plot instead of a line.我希望第二个 Y 轴上显示的数据是散点图而不是线。 Is there a way to do it?有没有办法做到这一点?

Thanks in advance.提前致谢。

You can set the mode to be "markers" instead of the default (which is "lines+markers" ):您可以将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,
)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM