Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly python how to draw unbounded lines and spans?

Tags:

python

plotly

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

like image 899
JavaNewbie Avatar asked Jul 01 '16 16:07

JavaNewbie


Video Answer


1 Answers

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,
                }
            }
        ],
    }
like image 123
Kieran Avatar answered Oct 05 '22 14:10

Kieran