10

Consider the following MWE to draw a scatter plot using the python API to plotly:

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])

enter image description here

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])

enter image description here

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?

3
  • 1
    Could you not just add another data set that has all the same values and plot that?
    – Jeeter
    Commented Aug 11, 2017 at 16:16
  • 1
    @Jeeter sure you can, see my edit. I was hoping for a better approach than that. For example, using cufflinks+pandas one can add the hline and vline options to iplot to achieve this (see e.g. In[43] here). Isn't there an equivalent way when using only plotly?
    – glS
    Commented Aug 11, 2017 at 16:25
  • I actually don't have much experience with plotly, so I don't know. Hope you find your answer, though!
    – Jeeter
    Commented Aug 11, 2017 at 16:27

3 Answers 3

13

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:

plotly shapes

Additional reference:

  1. plotly shapes examples

  2. plotly shapes reference

3
  • exactly what I was looking for, thanks! By the way, why do you need to import cufflinks, plotly.tools and download_plotlyjs for this?
    – glS
    Commented Aug 11, 2017 at 19:00
  • @glS I edited my code, those are brought when I copied from another cell, please refer to the updated code. Commented Aug 11, 2017 at 19:03
  • How can I add a label to the horizontal line? name doesn't appear to work Commented Jun 28, 2020 at 2:05
10

Alternatively, you could use the add_shape method, see the doc here. If you add the following code, you could add the line same as y=4 as above.

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

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.