我有一個包含超過 7 天的隨機資料的資料框,每個資料點都由 DatetimeIndex 索引。我想在一個圖上繪制每天的資料。目前我的嘗試如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
n =10000
i = pd.date_range('2018-04-09', periods=n, freq='1min')
ts = pd.DataFrame({'A': [np.random.randn() for i in range(n)]}, index=i)
dates = list(ts.index.map(lambda t: t.date).unique())
for date in dates:
ts['A'].loc[date.strftime('%Y-%m-%d')].plot()
結果如下:

正如您所看到的,當使用 DatetimeIndex 時,會保留相應的日期,這就是為什么我們每天都要回到下一天。
問題:
1-如何將當前代碼修復為具有從午夜開始到下一個午夜結束的 x 軸。
2-有沒有一種熊貓方法可以更好地分組日子并在一天內繪制它們而不使用 for 回圈?
uj5u.com熱心網友回復:
您可以將索引拆分為日期和時間,
或在單獨的圖表中:
axs = df.plot(subplots=True, title=df.columns.tolist(), legend=False, figsize=(6,8))
axs[0].figure.execute_constrained_layout()

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/406730.html
標籤:
下一篇:重疊的熱圖無法正確顯示
