Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Plotly:如何使用datetime索引以直线为中心绘制范围?_Python_Plotly_Plotly Python - Fatal编程技术网

Python Plotly:如何使用datetime索引以直线为中心绘制范围?

Python Plotly:如何使用datetime索引以直线为中心绘制范围?,python,plotly,plotly-python,Python,Plotly,Plotly Python,我想画一条线,周围有一个范围,如下图所示: 我发布了一个原始问题,但没有指定索引为datetime索引。我以为这不重要,但我错了 有一个答案用一个数字索引覆盖了它: 和文件在此: 但datetime索引的问题并未涉及 以下是一些测试数据: timestamp price min mean max 1596267946298 100.0 100 100.5 101 1596267946299 101.0 100 100.5 101 15962679

我想画一条线,周围有一个范围,如下图所示:

我发布了一个原始问题,但没有指定索引为datetime索引。我以为这不重要,但我错了

有一个答案用一个数字索引覆盖了它:

和文件在此:

但datetime索引的问题并未涉及

以下是一些测试数据:

timestamp      price   min  mean   max  
1596267946298  100.0   100  100.5  101
1596267946299  101.0   100  100.5  101
1596267946300  102.0   98   99.5   102
1596267948301  99.0    98   99.5   102
1596267948302  98.0    98   99.5   102
1596267949303  99.0    98   995.   102
我希望乐队从最小到最大覆盖,中间画出平均值

另一个选项是从上面发布的问题()的第一个答案中提取代码,并将数据生成更改为:

index = pd.date_range('1/1/2000', periods=25, freq='T')
df = pd.DataFrame(dict(A=np.random.uniform(low=-1, high=2, size=25).tolist(),
                       B=np.random.uniform(low=-4, high=3, size=25).tolist(),
                       C=np.random.uniform(low=-1, high=3, size=25).tolist()),
                  index=index)

这将以相同的方式工作,但会创建一个日期时间索引。

与中的设置相比,导致问题的是,
x+x[::-1]
与日期时间索引不太兼容。但是如果您在中设置
x=df.index

# add line and shaded area for each series and standards deviation
for i, col in enumerate(df):
    new_col = next(line_color)
    # x = list(df.index.values+1)
    x = df.index
然后将
x+x[:-1]
替换为
x=x.append(x[:-1])

那么事情就应该进展顺利了

绘图:

完整代码:
我的建议对你效果如何?我成功了!谢谢很乐意帮忙!谢谢你接受我的回答。
# standard deviation area
fig.add_traces(go.Scatter(
                            #x+x[::-1],
                            x=x.append(x[::-1]),
# imports
import plotly.graph_objs as go
import plotly.express as px
import pandas as pd
import numpy as np

# sample data in a pandas dataframe
np.random.seed(1)
df=pd.DataFrame(dict(A=np.random.uniform(low=-1, high=2, size=25).tolist(),
                    B=np.random.uniform(low=-4, high=3, size=25).tolist(),
                    C=np.random.uniform(low=-1, high=3, size=25).tolist(),
                    ))
df = df.cumsum()

# set daterange as index
df['dates'] = pd.date_range('2020', freq='D', periods=len(df))
df.set_index('dates', inplace=True)

# ---

# define colors as a list 
colors = px.colors.qualitative.Plotly

# convert plotly hex colors to rgba to enable transparency adjustments
def hex_rgba(hex, transparency):
    col_hex = hex.lstrip('#')
    col_rgb = list(int(col_hex[i:i+2], 16) for i in (0, 2, 4))
    col_rgb.extend([transparency])
    areacol = tuple(col_rgb)
    return areacol

rgba = [hex_rgba(c, transparency=0.2) for c in colors]
colCycle = ['rgba'+str(elem) for elem in rgba]

# Make sure the colors run in cycles if there are more lines than colors
def next_col(cols):
    while True:
        for col in cols:
            yield col
line_color=next_col(cols=colCycle)

# plotly  figure
fig = go.Figure()

# add line and shaded area for each series and standards deviation
for i, col in enumerate(df):
    new_col = next(line_color)
    x = df.index
    y1 = df[col]
    y1_upper = [(y + np.std(df[col])) for y in df[col]]
    y1_lower = [(y - np.std(df[col])) for y in df[col]]
    y1_lower = y1_lower[::-1]

    # standard deviation area
    fig.add_traces(go.Scatter(
                                #x+x[::-1],
                                x=x.append(x[::-1]),
                                y=y1_upper+y1_lower,
                                fill='tozerox',
                                fillcolor=new_col,
                                line=dict(color='rgba(255,255,255,0)'),
                                showlegend=False,
                                name=col))

    # line trace
    fig.add_traces(go.Scatter(x=df.index,
                              y=y1,
                              line=dict(color=new_col, width=2.5),
                              mode='lines',
                              name=col)
                                )
fig.update_layout(xaxis=dict(range=[df.index[1],df.index[-1]]))
fig.show()