简体   繁体   English

如何在 plotly 中向散点图添加一条线?

[英]How can I add a single line to a scatter plot in plotly?

Consider the following MWE to draw a scatter plot using the python API to plotly:考虑以下 MWE 使用 python API 绘制散点图来绘制:

import plotly.plotly as py
import plotly.graph_objs
import plotly.offline
plotly.offline.init_notebook_mode()

data = list(range(10))
trace = plotly.graph_objs.Scatter(
    x=list(range(len(data))),
    y=data
)
plotly.offline.iplot([trace])

在此处输入图片说明

What if I now want to add a (say) horizontal line to this plot?如果我现在想在这个图中添加一条(比如说)水平线怎么办? I went through the documentation, for example the section on line and scatter and that on line charts , but none of the examples seem to cover how to overlay different plots, or simply draw straight lines and similar shapes.我浏览了文档,例如有关折线图和散点图的部分以及折线图的部分,但似乎没有一个示例涵盖如何覆盖不同的图,或者只是绘制直线和类似的形状。

A naive approach to do this is to just add the line as a second scatter plot, like the following:一种简单的方法是将线添加为第二个散点图,如下所示:

import plotly.plotly as py
import plotly.graph_objs
import plotly.offline
plotly.offline.init_notebook_mode()

data = list(range(10))
trace = plotly.graph_objs.Scatter(
    x=list(range(len(data))),
    y=data
)
trace_line = plotly.graph_objs.Scatter(
    x=list(range(len(data))),
    y=[4] * len(data),
    mode='lines'
)
plotly.offline.iplot([trace, trace_line])

在此处输入图片说明

This approach seems however to be suboptimal: aside for the verbosity required to add a single line, it also makes me manually "sample" the straight line, and it adds the line height to the tooltip on mouse hover.然而,这种方法似乎并不理想:除了添加单行所需的冗长之外,它还让我手动“采样”直线,并在鼠标悬停时将线高添加到工具提示中。

Is there a better approach to achieve this?有没有更好的方法来实现这一目标?

Hi from your question I can see that you need plotly shapes functionality and generate a horizontal line for the plot.嗨,从您的问题中,我可以看到您需要绘图形状功能并为绘图生成一条水平线。

Please find below the code for doing the same graph you have shown in the question请在下面的代码中找到您在问题中显示的相同图表

Code:代码:

from plotly.offline import iplot
import plotly.graph_objs as go


data = list(range(10))
trace = go.Scatter(
    x=list(range(len(data))),
    y=data
)
layout = {
    'shapes': [
        # Line Horizontal
        {
            'type': 'line',
            'x0': 0,
            'y0': 4,
            'x1': 10,
            'y1': 4,
            'line': {
                'color': 'rgb(50, 171, 96)',
                'width': 4
            },
        }
    ],
    'showlegend': True
}

fig = {
    'data': [trace],
    'layout': layout,
}


iplot(fig)

Output:输出:

情节形状

Additional reference:补充参考:

  1. plotly shapes examples情节形状示例

  2. plotly shapes reference情节形状参考

Alternatively, you could use the add_shape method, see the doc here.或者,您可以使用 add_shape 方法,请参阅此处的文档 If you add the following code, you could add the line same as y=4 as above.如果添加以下代码,则可以添加与上述 y=4 相同的行。

fig.add_shape(type="line",
              x0=4, 
              y0=0, 
              x1=4, 
              y1=10)

You can just add the next line:您只需添加下一行:

fig.add_hline(y=4, line_width=2, line_dash='dash')

Also checkout the documentation for further deep into the features that plotly recently has added.还可以查看文档以进一步深入了解 plotly 最近添加的功能。

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

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