gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:58:45 29 4
gpt4 key购买 nike

我试图在下面的 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

最佳答案

如果您想添加一条完全覆盖整个 x 轴的水平线,您可以使用隐藏轴技巧(受 this post 启发):

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

# add a second axis that overlays the existing one
fig.layout.xaxis2 = go.layout.XAxis(overlaying='x', range=[0, 2], showticklabels=False)
fig.add_scatter(x = [0, 2], y = [20, 20], mode='lines', xaxis='x2',
showlegend=False, line=dict(dash='dash', color = "firebrick", width = 2))

fig.show()

因此,输出图形将如下所示: enter image description here

关于Python Plotly - 向箱线图添加水平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58679441/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com