绘制时间序列

时间:2014-10-04 20:11:57

标签: matplotlib pandas

我正在尝试绘制逐分钟的股票数据,我可以将其作为熊猫系列。库存数据可在上午9:30至下午4:00之间使用。当我绘制它时,我得到这样的东西:time series

有没有办法在下班后避免插值?

1 个答案:

答案 0 :(得分:1)

您需要为不包括市场关闭期间的地块构建您自己的轴。这很繁琐。示例如下:

import pandas as pd
import matplotlib.pyplot as plt
import calendar
from matplotlib.ticker import FixedLocator

# --- let's fake up some data:
drng = pd.period_range('2015-04-01 00:00', '2015-04-02 23:59', freq='1min')
df = pd.DataFrame({'data':np.random.randn(len(drng))}, index=drng)
df['data'] = df.data.cumsum()
# let's only keep the fake data for when the market is open
# market opens at 9.30am and closes at 4pm.
df = df[((df.index.hour >= 10) | 
        ((df.index.hour == 9) & (df.index.minute >= 30))) &
        (df.index.hour <= 15)]

# --- we will need to construct our own index and labels for matplotlib
#     this is fiddly ... and will vary depending on period being plotted
#     this works for two days of data ... but you will want to vary for 
#     shorter or longer periods ...
df['year'] = df.index.year
df['month'] = pd.Series(df.index.month, index=df.index
    ).apply(lambda x: calendar.month_abbr[x])
df['day'] = df.index.day
df['hour'] = df.index.hour
df['minute'] = df.index.minute
df.index = range(len(df))

minorticks = df[df['minute'] == 0].index.tolist() # hours
majorticks = df[df['day'] != df['day'].shift()].index.tolist() # days

minorlabels = pd.Series(df.loc[minorticks, 'hour'].astype(str)).tolist()
majorlabels = pd.Series('\n' + df.loc[majorticks, 'day'].astype(str) + ' ' +
                df.loc[majorticks, 'month'].astype(str) + ' ' +
                df.loc[majorticks, 'year'].astype(str)).tolist()

# --- and plot
(fig, ax) = plt.subplots(figsize=(8, 4))
df['data'].plot(ax = ax)
ax.xaxis.set_major_locator(FixedLocator(majorticks))
ax.xaxis.set_minor_locator(FixedLocator(minorticks))
ax.set_xticklabels(minorlabels, minor=True)
ax.set_xticklabels(majorlabels, minor=False)
ax.set_xlabel('Time and Date')
ax.set_ylabel('Index')
fig.suptitle('Fake Market Data - without closed time periods')
fig.tight_layout(pad=2)
plt.show()

Example solution for a two-day plot