gpt4 book ai didi

python - Plotly:如何在不循环遍历数据集的情况下突出显示周末?

转载 作者:行者123 更新时间:2023-12-02 01:59:28 26 4
gpt4 key购买 nike

我正在尝试使用 plotly 绘制三个不同的时间序列数据帧(每个大约 60000 条记录),同时使用不同的背景颜色突出显示周末(和工作时间)。

有没有办法像 this solution 中提到的那样在不遍历整个数据集的情况下做到这一点? .虽然此方法可能有效,但在大型数据集上的性能可能很差

最佳答案

我会考虑使用 make_subplots 并将 go.Scatter 轨迹附加到次要 y 轴以充当背景颜色而不是指示周末的形状。

基本代码元素:

fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Scatter(x=df['date'], y=df.weekend,
fill = 'tonexty', fillcolor = 'rgba(99, 110, 250, 0.2)',
line_shape = 'hv', line_color = 'rgba(0,0,0,0)',
showlegend = False
),
row = 1, col = 1, secondary_y=True)

plotly :

enter image description here

完整代码:

import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import datetime
from plotly.subplots import make_subplots

pd.set_option('display.max_rows', None)

# data sample
cols = ['signal']
nperiods = 50
np.random.seed(2)
df = pd.DataFrame(np.random.randint(-1, 2, size=(nperiods, len(cols))),
columns=cols)
datelist = pd.date_range(datetime.datetime(2020, 1, 1).strftime('%Y-%m-%d'),periods=nperiods).tolist()
df['date'] = datelist
df = df.set_index(['date'])
df.index = pd.to_datetime(df.index)
df.iloc[0] = 0
df = df.cumsum().reset_index()
df['signal'] = df['signal'] + 100
df['weekend'] = np.where((df.date.dt.weekday == 5) | (df.date.dt.weekday == 6), 1, 0 )

fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Scatter(x=df['date'], y=df.weekend,
fill = 'tonexty', fillcolor = 'rgba(99, 110, 250, 0.2)',
line_shape = 'hv', line_color = 'rgba(0,0,0,0)',
showlegend = False
),
row = 1, col = 1, secondary_y=True)

fig.update_xaxes(showgrid=False)#, gridwidth=1, gridcolor='rgba(0,0,255,0.1)')
fig.update_layout(yaxis2_range=[-0,0.1], yaxis2_showgrid=False, yaxis2_tickfont_color = 'rgba(0,0,0,0)')
fig.add_trace(go.Scatter(x=df['date'], y = df.signal, line_color = 'blue'), secondary_y = False)

fig.show()

速度测试:

对于我系统上下面代码片段中的 nperiods = 2000%%timeit 返回:

162 ms ± 1.59 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

我最初建议使用 fig.add_shape() 的方法相当慢:

49.2 s ± 2.18 s per loop (mean ± std. dev. of 7 runs, 1 loop each)

关于python - Plotly:如何在不循环遍历数据集的情况下突出显示周末?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69162561/

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