Abd*_*man 2 python time-series plotly plotly-python
我正在尝试使用plotly 绘制三个不同的时间序列数据帧(每个数据帧大约有60000 条记录),同时使用不同的背景颜色突出显示周末(和工作时间)。
有没有一种方法可以在不循环遍历整个数据集的情况下完成此解决方案中提到的操作。虽然此方法可能有效,但在大型数据集上性能可能较差
我会考虑使用make_subplots并将go.Scatter跟踪附加到辅助 y 轴作为背景颜色而不是形状来指示周末。
fig = make_subplots(specs=[[{"secondary_y": True}]])\nfig.add_trace(go.Scatter(x=df[\'date\'], y=df.weekend,\n fill = \'tonexty\', fillcolor = \'rgba(99, 110, 250, 0.2)\',\n line_shape = \'hv\', line_color = \'rgba(0,0,0,0)\',\n showlegend = False\n ),\n row = 1, col = 1, secondary_y=True)\nRun Code Online (Sandbox Code Playgroud)\nimport numpy as np\nimport pandas as pd\nimport plotly.graph_objects as go\nimport plotly.express as px\nimport datetime\nfrom plotly.subplots import make_subplots\n\npd.set_option(\'display.max_rows\', None)\n\n# data sample\ncols = [\'signal\']\nnperiods = 50\nnp.random.seed(2)\ndf = pd.DataFrame(np.random.randint(-1, 2, size=(nperiods, len(cols))),\n columns=cols)\ndatelist = pd.date_range(datetime.datetime(2020, 1, 1).strftime(\'%Y-%m-%d\'),periods=nperiods).tolist()\ndf[\'date\'] = datelist \ndf = df.set_index([\'date\'])\ndf.index = pd.to_datetime(df.index)\ndf.iloc[0] = 0\ndf = df.cumsum().reset_index()\ndf[\'signal\'] = df[\'signal\'] + 100\ndf[\'weekend\'] = np.where((df.date.dt.weekday == 5) | (df.date.dt.weekday == 6), 1, 0 )\n\nfig = make_subplots(specs=[[{"secondary_y": True}]])\nfig.add_trace(go.Scatter(x=df[\'date\'], y=df.weekend,\n fill = \'tonexty\', fillcolor = \'rgba(99, 110, 250, 0.2)\',\n line_shape = \'hv\', line_color = \'rgba(0,0,0,0)\',\n showlegend = False\n ),\n row = 1, col = 1, secondary_y=True)\n\nfig.update_xaxes(showgrid=False)#, gridwidth=1, gridcolor=\'rgba(0,0,255,0.1)\')\nfig.update_layout(yaxis2_range=[-0,0.1], yaxis2_showgrid=False, yaxis2_tickfont_color = \'rgba(0,0,0,0)\')\nfig.add_trace(go.Scatter(x=df[\'date\'], y = df.signal, line_color = \'blue\'), secondary_y = False)\n\nfig.show()\nRun Code Online (Sandbox Code Playgroud)\n对于nperiods = 2000我的系统上的下面的代码片段,%%timeit返回:
162 ms \xc2\xb1 1.59 ms per loop (mean \xc2\xb1 std. dev. of 7 runs, 10 loops each)\nRun Code Online (Sandbox Code Playgroud)\n我最初建议使用的方法fig.add_shape()要慢得多:
49.2 s \xc2\xb1 2.18 s per loop (mean \xc2\xb1 std. dev. of 7 runs, 1 loop each)\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2344 次 |
| 最近记录: |