Python Plotly-向箱形图添加水平线

时间:2019-11-03 10:51:32

标签: plotly-python

我试图在y = 20处向下面的图中添加水平线,但没有成功。

感谢您的帮助!

import plotly.express as px
tips = px.data.tips()
fig = px.box(tips, x="time", y="total_bill")
fig.show()

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以像这样添加带有一行的自定义标记:

go.Scatter(y=[20], x=["Dinner"], mode="markers", marker_symbol=41, marker_line_color="red", marker_color="lightskyblue",
                           marker_line_width=3, marker_size=30, name="max")

另见此处:https://plotly.com/python/marker-style/

答案 1 :(得分:1)

您可以使用add_trace添加一条水平线:

import plotly.express as px

tips = px.data.tips()
fig = px.box(tips, x="time", y="total_bill")
fig.add_trace(go.Scatter(x=['Dinner', 'Lunch'], y=[20, 20], mode="lines", name=""))
fig.show()

它产生:horizontal line on a boxplot