1. 程式人生 > >matplotlib如何在繪製時間序列時跳過無資料的區間

matplotlib如何在繪製時間序列時跳過無資料的區間

其實官方文件裡就提供了方法,這裡簡單的翻譯並記錄一下.

11.1.9 Skip dates where there is no data
When plotting time series, e.g., financial time series, one often wants to leave out days on which there is no data, e.g., weekends.
By passing in dates on the x-xaxis, you get large horizontal gaps on periods when there is not data.
The solution is to pass in some proxy x-data, e.g., evenly sampled indices, and then use a custom formatter to format these as dates.
The example below shows how to use an ‘index formatter’ to achieve the desired plot:

解決方案是通過傳遞x軸資料的代理,比如下標,
然後通過自定義的’formatter’去取到相對應的時間資訊
manual內示例程式碼:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.ticker as ticker

#讀資料
r = mlab.csv2rec('../data/aapl.csv')
r.sort()
r = r[-30:] # get the last 30 days
N = len(r)
ind = np.arange(N) # the evenly spaced plot indices
def format_date(x, pos=None): #保證下標不越界,很重要,越界會導致最終plot座標軸label無顯示 thisind = np.clip(int(x+0.5), 0, N-1) return r.date[thisind].strftime('%Y-%m-%d') fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.plot(ind, r.adj_close, 'o-') ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date)) fig.autofmt_xdate() plt.show()

示例:
同樣一段資料上為原始,下為去掉無資料間隔區間
這裡寫圖片描述

import pandas as PD
import numpy as NP
import matplotlib.pyplot as PLT
import matplotlib.ticker as MTK

file = r'vix_series.csv'
df = PD.read_csv(file, parse_dates=[0, 2])
#用下標代理原始時間戳資料
idx_pxy = NP.arange(df.shape[0])
#下標-時間轉換func
def x_fmt_func(x, pos=None):
    idx = NP.clip(int(x+0.5), 0, df.shape[0]-1)
    return df['datetime'].iat[idx]
#繪圖流程
def decorateAx(ax, xs, ys, x_func):
    ax.plot(xs, ys, color="green", linewidth=1, linestyle="-")
    ax.plot(ax.get_xlim(), [0,0], color="blue", 
            linewidth=0.5, linestyle="--")
    if x_func:
        #set資料代理func
        ax.xaxis.set_major_formatter(MTK.FuncFormatter(x_func))
    ax.grid(True)
    return

fig = PLT.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
decorateAx(ax1, df['datetime'], df['vix_all'], None)
decorateAx(ax2, idx_pxy, df['vix_all'], x_fmt_func)
#優化label顯示,非必須
fig.autofmt_xdate()
PLT.show()

很多時候亂翻google還不如好好通讀官方manual…