2017-10-19 19 views
1

Comment tracer une ligne verticale en mode déconnecté, en utilisant python? Je veux ajouter des lignes à x = 20, x = 40 et x = 60, le tout dans la même parcelle.Comment tracer des lignes verticales dans l'intrigue hors ligne?

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

Répondre

3

Vous pouvez ajouter line s via shape dans layout, par exemple

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) 

vous donnerait

enter image description here

+0

a dû changer y1 à 16, mais il fait l'affaire! –

+1

Vous pouvez également utiliser 'yref: 'paper' qui force les coordonnées à être relatives à la grille et non relatives à vos valeurs. –