如何在没有breaktime的情况下绘制数据帧

时间:2015-12-27 12:29:14

标签: python pandas matplotlib

我从文件中加载了pandas DataFrame。当我绘制它时,它以直线显示所有时间,但我不需要休息时间的中间。怎么办?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import dateutil,os

str_today='2015-12-23'
pk_file='D:/20151223_SHSE.000001.pk'

start_tradetime=str_today+' 9:30:00';end_tradetime=str_today+' 15:00:00'
f_inx=pd.date_range(start_tradetime,end_tradetime,freq='5s')

symbol='SHSE.000001'

def load_tick_data(pk_file):
    fn=pk_file
    if os.path.exists(fn):
        return pd.read_pickle(fn)
    else:
        return None

symdata=load_tick_data(pk_file)
symdata.head(10)

plt.ion()
plt.figure(1)
ax1 = plt.subplot2grid((4,1), (0,0), colspan=3,rowspan=3)
#draw
symdata['last_price'].asof(f_inx).plot(ax=ax1)

how to remove the line

full ipython notebook runtime

1 个答案:

答案 0 :(得分:2)

有助于将DateTimeIndex转换为string表示,如下所示 - 系列跳转以显示“缺少”#39;交易休息的价值:enter image description here

df = pd.Series(data= np.random.random(size=400), index=pd.date_range(start=datetime(2015,1,1,9,0,0), freq='1Min', periods=400)).cumsum()
df.loc[datetime(2015, 1, 1, 11, 30, 0): datetime(2015, 1, 1, 13, 0, 0)] = np.nan
df.index = df.index.to_series().apply(lambda x: x.strftime('%H:%M'))
df.dropna().plot.line()