Plotly python how to draw unbounded lines and spans?
Asked Answered
J

4

8

I am using plotly (the offline version) within an IPython notebook and I like it a lot. However I couldn't find a way to plot a vertical line or a vertical band.

The equivalents in matplotlib are:

import matplotlib.plyplot as plt
plt.axvline(x=0)
plt.axvspan(xmin=0, xmax=1)

thanks in advance

Jenellejenesia answered 1/7, 2016 at 16:45 Comment(0)
K
6

You can add shapes to your plotly layout. Shapes can include lines or rectangles. They can also be made unbounded by drawing them relative to the plotting area rather than a particular axis. Have a look through the examples in the plotly shapes docs.

layout = {
        'title': "My Chart",
        'shapes': [
            {  # Unbounded line at x = 4
                'type': 'line',
                # x-reference is assigned to the x-values
                'xref': 'x',
                # y-reference is assigned to the plot paper [0,1]
                'yref': 'paper',
                'x0': 4,
                'y0': 0,
                'x1': 4,
                'y1': 1,
                'line': {
                    'color': 'rgb(55, 128, 191)',
                    'width': 3,
                }
            },
            {  # Unbounded span at 6 <= x <= 8
                'type': 'rect',
                # x-reference is assigned to the x-values
                'xref': 'x',
                # y-reference is assigned to the plot paper [0,1]
                'yref': 'paper',
                'x0': 6,
                'y0': 0,
                'x1': 8,
                'y1': 1,
                'fillcolor': '#d3d3d3',
                'opacity': 0.2,
                'line': {
                    'width': 0,
                }
            }
        ],
    }
Kisor answered 11/4, 2017 at 7:29 Comment(2)
Is there any way to extend this to an unbounded line?Pastelist
@IvoMerchiers Yes, you'd use the same principle - for example, for an unbounded horizontal line, you'd use 'xref': 'paper', 'x0': 0, 'x1': 1.Farrar
E
1

Check these:

fig.add_vline()
fig.add_hline()
fig.add_vrect()
fig.add_hrect()
Europe answered 24/6, 2024 at 17:3 Comment(0)
S
0

I'm just starting out with plotly myself, and so far I haven't found a good way to do this. But if you only need one horizontal or vertical line, the code below seems to be a workable hack, The idea is to use the default grid, but only plot a single grid line at the desired height. Include the following in your layout dict, and it will plot a horizontal line at yline.

  yaxis=dict(
         zeroline=False,
         autotick=False,
         showgrid=True,
         tick0=yline,
         dtick=<something very large, so that next lines are out of range>
  )
Stereoisomerism answered 26/7, 2016 at 19:34 Comment(0)
Z
0

I would use the 'shapes' option in the layout. For example, to get a vertical line at x = 6:

layout = {'title' : 'example',
          'shapes' : [{'type' : 'line', 'x0' : 6, 
                      'x1' : 6, 'y0' : 0, 'y1' : 10, 
                       'width' : 1}]}

You could change the width parameter to plot a vertical band.

Zollie answered 11/10, 2016 at 21:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.