Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: How to add a horizontal line to a line graph?

I made a line graph with the code below and I'm trying to add a horizontal line at y=1. I tried following the instructions on the plotly site but it is still not showing. Does anyone know why?

date = can_tot_df.date
growth_factor = can_tot_df.growth_factor

trace0 = go.Scatter(
            x=date,
            y=growth_factor,
            mode = 'lines',
            name = 'growth_factor'
)

fig = go.Figure()
fig.add_shape(
        type='line',
        x0=date.min(),
        y0=1,
        x1=date.max(),
        y1=1,
        line=dict(
            color='Red',
        )
)


data = [trace0]
iplot(data)
like image 464
gboge Avatar asked Mar 28 '20 21:03

gboge


People also ask

How do you add a line in plotly?

Line Plot with Plotly Express Plotly have express. line() – function to create a line plot. The line() – function takes two list's as parameters to plot in X, Y axes OR You can name the data set and put the names of the columns on the X & Y axes. There's an optional parameter called title, a title for the plot.

How do you get a horizontal line on a graph?

Horizontal lines have zero slope. To graph a horizontal line in the standard coordinate system, use the equation y = k , y = k , y=k, where k gives the point on the y-axis that the line will intersect.

How do you plot two lines on the same graph in plotly?

To create multiple line charts on the same plot with plotly graph objects, all you have to do is add another trace to the plot.


2 Answers

Short answer, and a general solution:

fig.add_shape(type='line',
                x0=0,
                y0=40,
                x1=8,
                y1=40,
                line=dict(color='Red',),
                xref='x',
                yref='y'
)

Details and specifics about OP's question

It's hard to tell exactly what's wrong without a sample of your data. What I can tell for sure is that you're missing the arguments xref and yref to specify that the line is drawn as units of your y and x axis. Judging by your sample code, this is what you'd like to do since you're specifying your x-values in terms of dates.

Also, you don't need to worry about iplot for newer versions of plotly. You can display your chart just as easily by just running fig.show(). 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.

Plot:

enter image description here

Code:

import plotly.graph_objects as go
import numpy as np

x = np.arange(10)

fig = go.Figure(data=go.Scatter(x=x, y=x**2))

fig.add_shape(type='line',
                x0=0,
                y0=40,
                x1=8,
                y1=40,
                line=dict(color='Red',),
                xref='x',
                yref='y'
)


fig.show()

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.

like image 86
vestland Avatar answered Oct 21 '22 21:10

vestland


If you use subplots, then this is the easiest way I found to add an other line to a subplot. this example draws a horizontal line at y=80 for all x values

from plotly.subplots import make_subplots

fig = make_subplots(rows=2, cols=1,
                    shared_xaxes=True,
                    vertical_spacing=0.02)
[some graph]

fig.add_trace(go.Scatter(
        name='Y=80',
        x = [df['date'].min(), df['date'].max()],
        y = [80, 80],
        mode = "lines",
        marker = dict(color = 'rgba(80, 26, 80, 0.8)')
    ),row=1, col=1)

i found the solution on github :

like image 21
endo.anaconda Avatar answered Oct 21 '22 22:10

endo.anaconda