1
Q
绘制时间序列
A
回答
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()
相关问题
- 1. 绘制时间序列Julia
- 2. 绘制时间序列
- 3. 用matplotlib绘制线条(时间序列)
- 4. 时间序列绘制在R
- 5. 绘制移动时间序列曲线
- 6. Plotly时间序列 - 水平线绘制
- 7. 浮图时间序列不绘制线
- 8. ggplot时间序列绘制:按日期
- 9. 在python中绘制时间序列?
- 10. 绘制时间序列的样本
- 11. 绘制停产时间序列对象
- 12. 如何绘制滞后时间序列?
- 13. 熊猫时间序列绘制
- 14. 使用Seaborn FacetGrid绘制时间序列
- 15. 使用R绘制时间序列
- 16. 如何绘制多个时间序列
- 17. 绘制时间序列数据
- 18. 绘制多个时间序列数据
- 19. 在ggplot2中绘制时间序列
- 20. 绘制的时间列表
- 21. 时间序列不绘图
- 22. 时间序列JFreeChart重绘时间轴
- 23. JFreechart绘制时间刻度上的时间序列
- 24. 绘制多个时间序列,单个日期序列和py_date()
- 25. 如何在R中绘制多个序列/时间序列?
- 26. 如何在R中绘制二进制状态时间序列?
- 27. 在R中绘制并保存时间序列列表
- 28. AMchart绘制阵列的时间序列数据
- 29. 创建增量列绘制R中的时间序列差异
- 30. 在R中绘制顺序(时间序列)数据的子集
这不是一个内插,只matplotlib绘制的两个点之间的线。为了克服这个问题,这两点之间至少应该有一个NaN,例如可以用'df.resample('1min')来实现' – joris 2014-10-05 10:32:00
是的,对于重采样,两点之间没有更多的线。但是,仍然有很多空的空间。有没有办法让它跳过?换句话说,在一天的16:00之后,直接跳过第二天的09:30? – vdesai 2014-10-05 13:16:30