我正在测试情节表达。
我有最新版本: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'
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?
我想做的就是开始add_vline工作。
这是操作指南的第二个示例:https://plotly.com/python/horizontal -vertical-shapes/
ves*_*and 20
从plotly 4.12版本开始,您似乎没有运行该版本,您可以添加 水平线和垂直线以及矩形。因此,对于您的情况,只需使用:
fig.add_vline()
Run Code Online (Sandbox Code Playgroud)
我这边你的代码没有任何问题。这个确切的片段:
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()
Run Code Online (Sandbox Code Playgroud)
...产生这个数字:
你正在运行哪个情节版本?
要运行最小的示例,您的包plotly和您的包都必须是最新的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()
Run Code Online (Sandbox Code Playgroud)
我引用了 您共享的文档。
add_hline、add_vline、add_hrect、 和add_vrect在图 4.12 中介绍。
请携带此版本或更新版本的软件包。