有没有办法让 add_vline 垂直线在 y 轴上无限延伸?

时间:2021-02-01 18:47:27

标签: python plotly

从 plotly 4.12 版开始,您可以添加水平线和垂直线以及矩形。我已经安装了 4.12。我正在使用以下代码段:

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()

...产生这个数字:

enter image description here

为什么 vline 不会在 y 轴上无限延伸?

此处github.com/plotly/plotly.py/issues/2979 描述了相同的问题,但未提供解决方案。我可以在此处找到解决方法的说明:Plotly: how to make an unbounded vertical line in a subplot?,但该解决方法非常冗长且不够简洁。

我真的很想使用 add_vline 功能,但我有点困惑,因为 documentation 清楚地说“向在 y- 中无限延伸的图或子图添加垂直线尺寸。”并且所有示例 here 也扩展到 1 以上:因此在我看来,预期的行为应该是使该行扩展到 1 以上。

1 个答案:

答案 0 :(得分:0)

add_vline 函数使用底层渲染引擎 Plotly.js 中最近添加的功能,这意味着无论您在何处使用 Plotly.py,都需要最新版本的 Plotly.js。在实践中,这意味着在 JupyterLab 中安装最新的 jupyterlab-plotly 扩展,在 Dash 中安装最新的 dash/dash_core_components,或者等到 VSCode 和/或 Streamlit 更新他们的 Plotly.js 板载副本,如果您在这些环境中看到这个问题。

可在此处找到更多信息:Troubleshooting | Python | Plotly

相关问题