如何在离线绘图中绘制垂直线? [英] How to plot vertical lines in plotly offline?

查看:96
本文介绍了如何在离线绘图中绘制垂直线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用python在离线绘图中绘制一条垂直线?我想在同一图中添加x = 20,x = 40和x = 60的线.

How would one plot a vertical line in plotly offline, using python? I want to add lines at x=20, x=40, and x=60, all in the same plot.

def graph_contracts(self):
    trace1 = go.Scatter(
        x=np.array(range(len(all_prices))),
        y=np.array(all_prices), mode='markers', marker=dict(size=10, color='rgba(152, 0, 0, .8)'))
    data = [trace1]
    layout = go.Layout(title='Market Contracts by Period',
                       xaxis=dict(title='Contract #',
                                  titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f')),
                       yaxis=dict(title='Prices ($)',
                                  titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f')))
    fig = go.Figure(data=data, layout=layout)
    py.offline.plot(fig)

推荐答案

您可以通过

You can add lines via shape in layout, e.g.

import plotly
plotly.offline.init_notebook_mode()
import random

x=[i for i in range(100)]
trace = plotly.graph_objs.Scatter(x=x,
                                  y=[random.random() for _ in x],
                                  mode='markers')
shapes = list()
for i in (20, 40, 60):
    shapes.append({'type': 'line',
                   'xref': 'x',
                   'yref': 'y',
                   'x0': i,
                   'y0': 0,
                   'x1': i,
                   'y1': 1})

layout = plotly.graph_objs.Layout(shapes=shapes)
fig = plotly.graph_objs.Figure(data=[trace],
                               layout=layout)
plotly.offline.plot(fig)

会给你

这篇关于如何在离线绘图中绘制垂直线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆