5

In plotly website, there is example that can use shape function add vertical or horizontal line in plotly.

import plotly.plotly as py
import plotly.graph_objs as go

trace0 = go.Scatter(
    x=[2, 3.5, 6],
    y=[1, 1.5, 1],
    mode='text',
)
data = [trace0]
layout = {
    'xaxis': {
        'range': [0, 7]
    },
    'yaxis': {
        'range': [0, 2.5]
    },
    'shapes': [
        # Line Horizontal
        {
            'type': 'line',
            'x0': 2,
            'y0': 2,
            'x1': 5,
            'y1': 2,
            'line': {
                'color': 'rgb(50, 171, 96)',
                'width': 4,
                'dash': 'dashdot',
            }
        }
    ]
}

fig = {
    'data': data,
    'layout': layout,
}

py.iplot(fig, filename='shapes-lines')

But I wonder if there is any ways to add legend for the horizontal lines.

3
  • But what I need is legend for the vertical line. Commented Jun 3, 2019 at 9:27
  • Please reference the example then. Thank you. Commented Jun 3, 2019 at 9:30
  • 3
    @Mike_H Is this really a duplicate? OP is asking for legends for shapes, and not the shapes themselves. Your suggested post includes a vertical line to show the median of specified groups. And as you can see, the median is not included in the legend. Commented Jun 3, 2019 at 11:39

1 Answer 1

2

I think the only option at the moment is to plot it as a Scatter trace.

For example this snippet of code

import plotly.graph_objects as pgo
fig = pgo.Figure()

fig.add_traces([
    pgo.Scatter(
        x=[2, 3.5, 6],
        y=[1, 1.5, 1],
        name='Yet Another Trace'
    ), 
    pgo.Scatter(
        x=[2,5],
        y=[2,2], 
        line={
            'color': 'rgb(50, 171, 96)',
            'width': 4,
            'dash': 'dashdot',
        }, name='Horizontal Line'
    )
])

fig.update_layout(**{
    'xaxis': {
        'range': [0, 7]
    },
    'yaxis': {
        'range': [0, 2.5]
    }
})

fig

generates this result:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.