簡體   English   中英

如何使用 Plotly 從 plot 中刪除周末?

[英]How remove weekends from plot using Plotly?

我試圖從這個時間序列 plot 中消除周末的差距。 x 軸是數據時間戳。 我已經嘗試過此站點上的代碼,但無法使其正常工作。 查看使用的示例文件

數據看起來像這樣

+-----------------------+---------------------+-------------+-------------+
|          asof         |    INSERTED_TIME    | DATA_SOURCE |    PRICE    |
+-----------------------+---------------------+-------------+-------------+
| 2020-06-17   00:00:00 | 2020-06-17 12:00:15 | DB          | 170.4261757 |
+-----------------------+---------------------+-------------+-------------+
| 2020-06-17   00:00:00 | 2020-06-17 12:06:10 | DB          | 168.9348656 |
+-----------------------+---------------------+-------------+-------------+
| 2020-06-17   00:00:00 | 2020-06-17 12:06:29 | DB          | 168.8412129 |
+-----------------------+---------------------+-------------+-------------+
| 2020-06-17   00:00:00 | 2020-06-17 12:07:27 | DB          | 169.878796  |
+-----------------------+---------------------+-------------+-------------+
| 2020-06-17   00:00:00 | 2020-06-17 12:10:28 | DB          | 169.3685879 |
+-----------------------+---------------------+-------------+-------------+
| 2020-06-17   00:00:00 | 2020-06-17 12:12:14 | DB          | 169.0787045 |
+-----------------------+---------------------+-------------+-------------+
| 2020-06-17   00:00:00 | 2020-06-17 12:12:33 | DB          | 169.7561092 |
+-----------------------+---------------------+-------------+-------------+

Plot 包括周末休息

使用 function,我得到下面的 plot,直線從周五結束到周一早上。 使用 px.scatter,我沒有得到這條線,但我仍然得到了差距。

import plotly.express as px
import pandas as pd

sampledf = pd.read_excel('sample.xlsx')

fig_sample = px.line(sampledf, x = 'INSERTED_TIME', y= 'PRICE', color = 'DATA_SOURCE')
fig_sample.show()

在此處輸入圖像描述

嘗試沒有周末休息

fig_sample = px.line(sampledf, x = 'INSERTED_TIME', y= 'PRICE', color = 'DATA_SOURCE')
fig_sample.update_xaxes(
    rangebreaks=[
        dict(bounds=["sat", "mon"]) #hide weekends
    ]
)
fig_sample.show()

在此處輸入圖像描述

使用范圍分隔符會導致空白plot

任何幫助表示贊賞。 謝謝

使用rangebreaks時有 1000 行的限制當使用超過 1000 行時,添加參數render_mode='svg'

在下面的代碼中,我使用了scatter function,但正如您所見,大的周末間隙不再存在。 此外,我已經排除了晚上 11 點到上午 11 點之間的時間

sampledf = pd.read_excel('sample.xlsx')

fig_sample = px.scatter(sampledf, x = 'INSERTED_TIME', y= 'PRICE', color = 'DATA_SOURCE', render_mode='svg')
fig_sample.update_xaxes(
    rangebreaks=[
        { 'pattern': 'day of week', 'bounds': [6, 1]}
        { 'pattern': 'hour', 'bounds':[23,11]}
    ]
)
fig_sample.show()

在此處輸入圖像描述

plot 中的值與原始數據集不同,但將適用於原始帖子中的數據。 在這里找到幫助

看起來空白 plot 上的 x 軸甚至沒有正確的范圍,因為它開始於不同的年份。 如果不查看確切的數據輸入,很難解釋這種行為,但您可以從一個工作的、更簡單的數據集開始並嘗試檢查差異(嘗試使用 select 點的數據過濾版本 plot 或檢查差異dtypes等的數據類型。

您將使用更簡單的數據集看到預期的行為:

import plotly.express as px
import pandas as pd
from datetime import datetime
d = {'col1': [datetime(2020, 5, d) for d in range(1, 30)],
     'col2': [d if (d + 3) % 7 not in (5, 6) else 0 for d in range(1, 30)]}
df = pd.DataFrame(data=d)
df.set_index('col1')

df_weekdays = df[df['col1'].dt.dayofweek.isin([0,1,2,3,4])]

f = px.line(df, x='col1', y='col2')
f.update_xaxes(
    rangebreaks=[
        dict(bounds=["sat", "mon"]), #hide weekends
    ]
)
f.show()

有休息

對於沒有周末的df_weekdays ,它是一個類似的圖像:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM