2017-10-19 58 views
1

如何使用python在陰謀離線中繪製垂直線?我想在x = 20,x = 40和x = 60處添加行,全部在同一個圖中。如何以陰謀離線繪製垂直線?

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) 

Generated graph

回答

3

您可以通過layout添加shapeline S,例如

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) 

會給你

enter image description here

+0

後來換y 1至16,但它的伎倆! –

+1

您也可以使用'yref:'紙張'強制座標相對於網格而不是相對於您的值。 –