简体   繁体   English

我如何使用 plotly plot 置信区间为 python 的线?

[英]How can I plot a line with a confidence interval in python using plotly?

I am trying to use plotly to plot a graph similar to the one here below:我正在尝试使用 plotly 到 plot 类似于下面的图表:

在此处输入图像描述

Unfortunately I am only able to plot something like this不幸的是,我只能 plot 这样的事情在此处输入图像描述

What I would like is to have normal boundaries (upper and lower defined by two dataframe columns and only one entry in the legend.我想要的是有正常边界(上限和下限由两个 dataframe 列定义,图例中只有一个条目。


    import plotly.graph_objs as go
    
    # Create a trace for the lower bound
    trace1 = go.Scatter(x=df.index, 
                        y=df['lower'], 
                        name='Lower Bound',
                        fill='tonexty',
                        fillcolor='rgba(255,0,0,0.2)',
                        line=dict(color='blue'))
    
    # Create a trace for the median
    trace2 = go.Scatter(x=df.index, 
                        y=df['median'], 
                        name='median',
                        line=dict(color='blue', width=2))
    
    
    # Create a trace for the upper bound
    trace3 = go.Scatter(x=df.index, 
                        y=df['upper'], 
                        name='Upper Bound',
                        fill='tonexty',
                        fillcolor='rgba(255,0,0,0.2)',
                        line=dict(color='blue'))
    
    # Create the layout
    layout = go.Layout(xaxis=dict(title='Date'), 
                       yaxis=dict(title='title'))
    
    # Create the figure with the three traces and the layout
    fig = go.Figure(data=[trace1, trace2, trace3], layout=layout)


    context['pltyplot'] = pltyplot(fig, output_type="div")

I want to use plotly because I am integrating the resulting figure into a django web page and plotly enables, with the las line, to import the whole object in a clean, simple and interactive way into the poge.我想使用 plotly,因为我将生成的图形集成到 django web 页面中,而 plotly 可以通过 las 行以干净、简单和交互的方式将整个 object 导入 poge。

Any ideas?有任何想法吗?

You can try this code:你可以试试这段代码:

import plotly.graph_objs as go

x = [1, 2, 3, 4, 5]
y = [2, 4, 5, 3, 6]

# Define the confidence interval
interval = 0.6 * np.std(y) / np.mean(y)

fig = go.Figure()

fig.add_trace(go.Scatter(x=x, y=y, mode='lines', name='Line'))


fig.add_trace(go.Scatter(x=x+x[::-1], 
                        y=y+[i + interval for i in y[::-1]],
                        fill='toself',
                        fillcolor='rgba(0,100,80,0.2)',
                        line=dict(width=0),
                        showlegend=False))

fig.add_trace(go.Scatter(x=x+x[::-1], 
                        y=y+[i - interval for i in y[::-1]],
                        fill='toself',
                        fillcolor='rgba(0,100,80,0.2)',
                        line=dict(width=0),
                        showlegend=False))

fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM