[英]Plotly: How to add vertical line to plotly plot?
我正在測試 plotly 快遞。
我有最新版本: 0.4.1
我正在嘗試 plot 他們教程中最基本的圖表,但它拋出了一個錯誤:
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 版本?
要運行最小示例,您的 package plotly 以及您的 package 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()
我引用了您共享的文檔。
在 plotly 4.12 中引入了
add_hline
、add_vline
、add_hrect
和add_vrect
。
請攜帶您的 package 使用此版本或更新版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.