19

I am testing plotly express.

I have the latest version: 0.4.1

I am trying to plot the most basic chart in their tutorial but it's throwing an error:

import plotly.express as px

df = px.data.iris()
fig = px.scatter(df, x="petal_length", y="petal_width")
fig.add_vline(x=2.5, line_width=3, line_dash="dash", line_color="green")
fig.add_hrect(y0=0.9, y1=2.6, line_width=0, fillcolor="red", opacity=0.2)
fig.show()


AttributeError: 'Figure' object has no attribute 'add_vline'

Am I doing something wrong?

All I want to do is to get add_vline working.

It's the second example on the how-to guide here: https://plotly.com/python/horizontal-vertical-shapes/

2
  • 3
    add_vline is introduced in plotly 4.12. Please update your package. Commented Jan 22, 2021 at 14:31
  • @mosc9575 Would you consider adding that as an answer? Commented Jan 22, 2021 at 14:46

2 Answers 2

29

As of plotly version 4.12, which you seem to not be running, you can add Horizontal and Vertical Lines and Rectangles. So for your case, just use:

fig.add_vline()

And there's nothing wrong with your code on my end. This exact snippet:

import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="petal_length", y="petal_width")
fig.add_vline(x=2.5, line_width=3, line_dash="dash", line_color="green")
fig.add_hrect(y0=0.9, y1=2.6, line_width=0, fillcolor="red", opacity=0.2)
fig.show()

...produces this figure:

enter image description here

Which plotly version are you running?

Sign up to request clarification or add additional context in comments.

7 Comments

why does the vline does not extend indefinetely on the y axis?
I am running into this issue as well with plotly 4.12 and would like the vline to extend indefinitely
Same problem described here github.com/plotly/plotly.py/issues/2979 but no solution is provided
thanks for the quick answer but the workaround is quite verbose and not so neat. I am a bit puzzled as doc plotly.com/python-api-reference/generated/… clearly says "Add a vertical line to a plot or subplot that extends infinitely in the y-dimension."
All examples here plotly.com/python/horizontal-vertical-shapes/… extends beyond 1 too
|
8

To run the minimal example your package plotly has to be up to date as well as your package plotly.express.

import plotly.express as px

df = px.data.iris()
fig = px.scatter(df, x="petal_length", y="petal_width")
fig.add_hline(y=0.9)
fig.add_vrect(x0=0.9, x1=2)
fig.show()

I quote from the documentation you have shared.

add_hline, add_vline, add_hrect, and add_vrect is introduced in plotly 4.12.

Please bring your package on this or a newer version.

1 Comment

My plotly-express was up to date. Look like I had to update plotly too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.