python - Plotly:如何在绘图中添加垂直线?

标签 python plotly

我正在测试 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()

...产生这个数字:

enter image description here

你正在运行哪个 plotly 版本?

关于python - Plotly:如何在绘图中添加垂直线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65846866/

相关文章:

python - 如何存储非英文文本?

python - BR 内的文本无法使用 python beautifulsoup 获取

python - 为什么 Queue.qsize 不准确?

python - 如何使用 Flask 渲染绘图列表

python - plotly :无法与本地 orca 服务器进程通信,即使服务器进程似乎正在运行

javascript - 绘制一条垂直线来显示平均值

python - 从 python 代码生成 UML 行为模型的开源工具?

python - 如何从 NumPy 数组中删除所有零元素?

r - 在 plotly 上修改热图中的标签 (x,y,z)?

javascript - 如何隐藏 plotly 中的数据差距?