gpt4 book ai didi

python - Plotly:如何在两条垂直线之间设置填充颜色?

转载 作者:行者123 更新时间:2023-12-03 15:54:10 32 4
gpt4 key购买 nike

使用 matplotlib,我们可以使用 fill_between()“简单地”填充两条垂直线之间的区域。就像在此 sample 中:
https://matplotlib.org/3.2.1/gallery/lines_bars_and_markers/fill_between_demo.html#selectively-marking-horizontal-regions-across-the-whole-axes
使用 matplotlib,我可以制作我需要的东西:
enter image description here
我们有两个信号,我正在计算滚动/移动 Pearson 和 Spearman 的相关性。当相关性低于 -0.5 或高于 0.5 时,我想对周期进行着色(Pearson 为蓝色,Spearman 为橙色)。我还在所有地块中用灰色将周末变暗。
但是,我发现使用 Plotly 很难完成相同的任务。了解如何在两条水平线之间进行操作也很有帮助。
请注意,我使用 Plotly 和 Dash 来加速多个绘图的可视化。用户要求更“动态类型的东西”。然而,我不是一个 GUI 人,不能花时间在这上面,尽管我需要用初步结果来喂养他们。
顺便说一句,我过去尝试过 Bokeh,但由于某种我不记得的原因而放弃了。 Plotly 看起来不错,因为我可以使用 Python 或 R,这是我的主要开发工具。
谢谢,
卡洛斯

最佳答案

我认为没有任何内置的 Plotly 方法相当于 matplotlib 的 fill_between()方法。但是,您可以绘制形状,因此可能的解决方法是绘制一个灰色矩形并设置参数 layer="below"以便信号仍然可见。您还可以在轴范围之外设置矩形的坐标,以确保矩形延伸到绘图的边缘。
您可以通过绘制矩形并以类似方式设置轴范围来填充水平线之间的区域。

import numpy as np
import plotly.graph_objects as go

x = np.arange(0, 4 * np.pi, 0.01)
y = np.sin(x)

fig = go.Figure()
fig.add_trace(go.Scatter(
x=x,
y=y
))

# hard-code the axes
fig.update_xaxes(range=[0, 4 * np.pi])
fig.update_yaxes(range=[-1.2, 1.2])

# specify the corners of the rectangles
fig.update_layout(
shapes=[
dict(
type="rect",
xref="x",
yref="y",
x0="4",
y0="-1.3",
x1="5",
y1="1.3",
fillcolor="lightgray",
opacity=0.4,
line_width=0,
layer="below"
),
dict(
type="rect",
xref="x",
yref="y",
x0="9",
y0="-1.3",
x1="10",
y1="1.3",
fillcolor="lightgray",
opacity=0.4,
line_width=0,
layer="below"
),
]
)

fig.show()
enter image description here

关于python - Plotly:如何在两条垂直线之间设置填充颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63494074/

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