我正在测试 plotly express。
我有最新版本:0.4.1
我正在尝试在他们的教程中绘制最基本的图表,但它抛出了一个错误:
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'
我做错了吗?
我只想得到add_vline
工作。
这是操作指南的第二个示例:https://plotly.com/python/horizontal-vertical-shapes/
最佳答案
从 plotly 4.12 版开始,您似乎没有在运行,您可以添加 Horizontal and Vertical Lines and Rectangles .因此,对于您的情况,只需使用:
fig.add_vline()
你的代码对我来说没有任何问题。这个确切的片段:
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()
...产生这个数字:
你正在运行哪个 plotly 版本?
关于python - Plotly:如何在绘图中添加垂直线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65846866/