如何使用plotly在条形图上绘制阈值线

0 python-3.x plotly

当前编写此代码可生成条形图,但想添加阈值线。有人可以帮我吗?

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")
Run Code Online (Sandbox Code Playgroud)

Slo*_*ner 5

尝试这样的事情。在图中,似乎通过提供了线条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',
                            },
                        },
                    ],
)
Run Code Online (Sandbox Code Playgroud)

您尚未提供示例数据,因此我尚未对此进行测试。为您的第一个问题提供代码做得很好,但是在Stack Overflow上,最好提供一个完全独立的示例,使人们可以按原样复制和运行。