gboge
gboge

Reputation: 453

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)

Upvotes: 27

Views: 72272

Answers (5)

Moritz Wittig
Moritz Wittig

Reputation: 321

You could also use fig.add_hline(y=1) --> see https://plotly.com/python/horizontal-vertical-shapes/

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_hline(y=40, line_width=3, line_dash="dash", line_color="green")


fig.show()

Upvotes: 28

vestland
vestland

Reputation: 61194

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.

Upvotes: 23

endo.anaconda
endo.anaconda

Reputation: 2468

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 :

Upvotes: 4

DSBLR
DSBLR

Reputation: 623

df = df
fig = px.scatter(df, x="date", y="growth_factor", mode = 'lines',
      hover_name=df['growth_factor'] )

fig.update_layout(shapes=[
dict(
  type= 'line',
  yref= 'y', y0= 1, y1= 1,   # adding a horizontal line at Y = 1
  xref= 'paper', x0= 0, x1= 1
     ) 
])

fig.show()

Upvotes: 2

s3dev
s3dev

Reputation: 9721

You’re adding the line to your fig object, but fig is not getting passed into the iplot() function, only your data. So only the trace is getting plotted.

If you're using a late version of plotly, the new syntax allows you to create this plot simply using the fig object, like:

from plotly import graph_objects as go

fig = go.Figure()

# Contrived dataset for example.
x = [1, 2, 3, 4]
y = [i**2 for i in x]

fig.add_trace(go.Scatter(
              x=x,
              y=y,
              mode = 'lines',
              name = 'growth_factor'))

fig.add_shape(type='line',
              x0=min(x),
              y0=5,
              x1=max(x),
              y1=5,
              line=dict(color='Red'))

fig.update_shapes(dict(xref='x', yref='y'))

fig.show()

Here are the plotly docs for convenience.

Upvotes: 2

Related Questions