[英]Python Plotly: Adding a horizontal line to a scatter plot that has multiple subplots
[英]Horizontal Line in Python Plotly Scatter plot
我正在尋找一種在 Plotly Scatter plot 中繪制兩條水平線的方法。 我的 x 軸索引不是固定的,並且每次都在變化。 所以我在 y = 5 和 y = 18 處尋找一條水平線,水平穿過圖表
我在這里尋找解決方案,但我不確定如何使用 Plotly express 的布局
我的分散代碼 plot:
import plotly.express as px
df = pd.DataFrame({"x":[0, 1, 2, 3, 4,6,8,10,12,15,18], "y":[0, 1, 4, 9, 16,13,14,18,19,5,12]})
fig = px.scatter(df, x="x", y="y")
fig
是的,您可以使用fig.update_layout()
來做到這一點,方法如下:
import pandas as pd
import plotly.express as px
df = pd.DataFrame({ "x":[0, 1, 2, 3, 4,6,8,10,12,15,18],
"y":[0, 1, 4, 9, 16,13,14,18,19,5,12]})
fig = px.scatter(df, x="x", y="y")
# add two horizontal lines
fig.update_layout(shapes=[
# adds line at y=5
dict(
type= 'line',
xref= 'paper', x0= 0, x1= 1,
yref= 'y', y0= 5, y1= 5,
),
# adds line at y=18
dict(
type= 'line',
xref= 'paper', x0= 0, x1= 1,
yref= 'y', y0= 18, y1= 18,
)
])
fig.show()
我不知道是否有更簡單的方法,但這就是我會使用的
這些天使用 vline 或 hline 更簡單
import plotly.express as px
df = pd.DataFrame({"x":[0, 1, 2, 3, 4,6,8,10,12,15,18], "y":[0, 1, 4, 9,
16,13,14,18,19,5,12]})
fig = px.scatter(df, x="x", y="y")
fig.add_hline(y=5)
fig.add_hline(y=18)
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.