Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot vertical lines in plotly offline?

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)

Generated graph

like image 554
gitpush_econ Avatar asked Oct 19 '17 22:10

gitpush_econ


People also ask

Can plotly be used offline?

Plotly allows you to generate graphs offline and save them in local machine. The plotly. offline. plot() function creates a standalone HTML that is saved locally and opened inside your web browser.

How do you insert a horizontal line in plotly?

The figure and code sample below will show you how to use fig. show() and how to define your lines in terms of axis units. An alternative to xref='x' is xref='paper' . Now you can specify x0 as a float between 0 and 1 spanning from the start and end of the plot.

What is the difference between plotly and Plotly Express?

Overview. The plotly. express module (usually imported as px ) contains functions that can create entire figures at once, and is referred to as Plotly Express or PX. Plotly Express is a built-in part of the plotly library, and is the recommended starting point for creating most common figures.

What is Iplot plotly?

Plotly is a free and open-source graphing library for Python. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.


1 Answers

This is my example. The most important instruction is this.

fig.add_trace(go.Scatter(x=[12, 12], y=[-300,300], mode="lines", name="SIGNAL"))

The most important attribute is MODE='LINES'.

Actually this example is about a segment with x=12

EXAMPLE

    import pandas as pd
    import plotly.graph_objects as go
    import matplotlib.pyplot as plt
    import numpy as np
    import plotly.tools as tls




    df1 = pd.read_csv('./jnjw_f8.csv')



    layout = go.Layout(
    xaxis = go.layout.XAxis(
        tickmode = 'linear',
        tick0 = 1,
        dtick = 3
    ),
    yaxis = go.layout.YAxis(
        tickmode = 'linear',
        tick0 = -100,
        dtick = 3
    ))

    fig = go.Figure(layout = layout)

    fig.add_trace(go.Scatter(x = df1['x'], y = 
    df1['y1'],name='JNJW_sqrt'))
    fig.add_trace(go.Scatter(x=[12, 12], y=[-300,300], 
    mode="lines", name="SIGNAL"))


    fig.show()

Look here too. how to plot a vertical line with plotly

like image 188
quine9997 Avatar answered Sep 21 '22 01:09

quine9997