[英]pandas - matplotlib: set xtick frequency with time data
我无法弄清楚如何使用时间数据更改 xtick 频率。 无论配方如何,错误都比比皆是。
样本原始数据:
datetime branch
2019-05-07 14:39:14 NOWHEREVILLE
2019-05-07 16:17:39 NOWHEREVILLE
2019-05-07 16:17:41 NOWHEREVILLE
2019-05-07 16:26:42 NOWHEREVILLE
2019-05-07 16:26:50 NOWHEREVILLE
按小时重新采样:
hourly_count = circ.resample('H').count()
hourly_count = hourly_count.rename(columns={'branch': 'circulation'})
hourly_count.head()
Output:
datetime circulation
2019-05-07 14:00:00 1
2019-05-07 15:00:00 0
2019-05-07 16:00:00 5
2019-05-07 17:00:00 0
2019-05-07 18:00:00 0
如果我只是这样做: hourly_count.plot()
,也许改变 plot 大小,一切正常。 但是 xtick 标记的频率放大不够。
我在 SO 上看到的所有食谱都明确设置了 x 轴。 我还没有找到一个不会引发错误的食谱。
例子:
hourly_count.plot(x=hourly_count.index, y='circulation', kind='line', lw=0.75, c='r')
抛出(片段):
KeyError Traceback (most recent call last)
<ipython-input-20-7f11c92c2c61> in <module>()
----> 1 hourly_count.plot(x=hourly_count.index, y='circulation', kind='line', lw=0.75, c='r')
/opt/anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in __call__(self, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
2939 fontsize=fontsize, colormap=colormap, table=table,
2940 yerr=yerr, xerr=xerr, secondary_y=secondary_y,
-> 2941 sort_columns=sort_columns, **kwds)
2942 __call__.__doc__ = plot_frame.__doc__
2943
/opt/anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in plot_frame(data, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
1975 yerr=yerr, xerr=xerr,
1976 secondary_y=secondary_y, sort_columns=sort_columns,
-> 1977 **kwds)
1978
1979
我在这里使用自己的数据进行跟踪: Advanced plotting with Pandas
一切正常,直到我到达上面的 plot 命令。
更新:
fig, ax = plt.subplots()
plt.plot(df.index, df['circulation'])
ax.set_xlim(df.index.min() - pd.Timedelta(hours=5), df.index.max() + pd.Timedelta(hours=5))
mytime = mdates.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(mytime)
ax.xaxis.set_major_locator(mdates.HourLocator(interval=1))
fig.autofmt_xdate()
Output:
IIUC,您可以将 set_xlim 与 timedelta 调整一起使用:
print(df)
datetime circulation
0 2019-05-07 14:00:00 1
1 2019-05-07 15:00:00 0
2 2019-05-07 16:00:00 5
3 2019-05-07 17:00:00 0
4 2019-05-07 18:00:00 0
df1 = df.set_index('datetime')
ax = df1.plot()
ax.set_xlim(df1.index.min() - pd.Timedelta(hours=5), df1.index.max() + pd.Timedelta(hours=5))
Output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.