olivialove
olivialove

Reputation: 1

How to do a threshold line on a bar chart using plotly

Currently written this code that produces a bar chart but would like to add a threshold line. Could anyone help me please?

def make_bar_chart(data):
    """Takes a list of dicts with a time and price"""
    # Times
    chart_x = []
    # Prices
    chart_y = []

    # Create the relevant arrays
    for item in data:
        chart_x.append(item["time"])
        chart_y.append(item["price"])

    # Make the chart
    the_graph = Bar(x = chart_x, y = chart_y , name = "Stocks")
    graph_data = Data([the_graph])

    the_layout = Layout(title = "Stocks", xaxis = dict(title = "Time"), yaxis = dict(title = "Price"))
    the_figure = Figure(data = graph_data, layout = the_layout)
    plotly.offline.plot(the_figure, filename = "stocks.html")

Upvotes: 0

Views: 2597

Answers (1)

SlowLearner
SlowLearner

Reputation: 7997

Try something like this. In plotly it seems that lines are provided via shapes.

the_layout = Layout(title = "Stocks",
                    xaxis = dict(title = "Time"),
                    yaxis = dict(title = "Price"),
                    shapes=[
                        {
                            'type': 'line',
                            'xref': 'paper',
                            'x0': 0,
                            'y0': 100, # use absolute value or variable here
                            'x1': 1,
                            'y1': 100, # ditto
                            'line': {
                                'color': 'rgb(50, 171, 96)',
                                'width': 1,
                                'dash': 'dash',
                            },
                        },
                    ],
)

I haven't tested this as you haven't provided sample data. Well done for supplying code on your first question, but on Stack Overflow it's best to provide a completely self-contained example that people can copy and run 'as is.'

Upvotes: 7

Related Questions