Python Plotly 散点图中的水平线 [英] Horizontal Line in Python Plotly Scatter plot
本文介绍了Python Plotly 散点图中的水平线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种在 Plotly Scatter 图中绘制两条水平线的方法.我的 x 轴索引不是固定的,每次都在不断变化.所以我正在寻找一条水平线在 y = 5 和 y = 18 处水平穿过图表
我在
我不知道是否有更简单的方法,但这是我会使用的
I'm looking for a way to draw two horizontal lines in a Plotly Scatter plot. My x-axis index is not fixed and keep changing everytime. So I'm looking for a Horizontal line at y = 5 and y = 18 passing horizontally across the chart
I looked here for a solution but I'm not sure how to use layouts with Plotly express
My code for scatter 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
解决方案
Yes, you can do that using fig.update_layout()
, here is how:
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()
Which produces this graph:
I don't know if there is an easier way, but this is what I would use
这篇关于Python Plotly 散点图中的水平线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文