Plotly 是否有类似 matplotlib 的功能 fill_between

Does Plotly similar function like matplotlib fill_between

我正在将一些 matplotlib 代码转换为 Plotly。

Plotly有没有类似matplotlib的东西fill_between

要转换的代码

    #   ax.fill_between(fcst_t, fcst['yhat_lower'], fcst['yhat_upper'],
    #   
                 color='#0072B2', alpha=0.2, label='Uncertainty interval')

我可以设法做类似下面的事情,但我想用特定颜色填充这两条线之间的区域。

    fig.add_trace(
           go.Scatter(x=fcst_t,y=forecast['yhat_lower'],fillcolor='grey',mode='lines+markers',name="yhat_lower"),
            row=1,col=1
    )

    fig.add_trace(
           go.Scatter(x=fcst_t,y=forecast['yhat_upper'],fillcolor='grey',mode='lines+markers',name="yhat_upper"),
            row=1,col=1
    )

go.Scatter 有一个 fill 关键字,可用于控制填充行为。您可以在此 documentation page 或输入 help(go.Scatter).

阅读更多内容
import plotly.graph_objects as go
import numpy as np

x = np.linspace(0, 10, 100)
y1 = np.sin(x) + 2
y2 = np.sin(x / 2) + 8
fig = go.Figure([
    go.Scatter(x=x, y=y1),
    # fill between the endpoints of this trace and the endpoints of the trace before it
    go.Scatter(x=x, y=y2, fill="tonextx"),
])
fig

如果两条线的点数不同,填充也应该有效!