pandas - Pandas df histo,格式化我的 x 代码并包含空
问题描述
我得到了这个熊猫df:
index TIME
12:07 2019-06-03 12:07:28
10:04 2019-06-04 10:04:25
11:14 2019-06-09 11:14:25
...
我使用这个命令做一个直方图来绘制每 15 分钟周期的发生次数
df['TIME'].groupby([df["TIME"].dt.hour, df["TIME"].dt.minute]).count().plot(kind="bar")
我的情节是这样的:
我怎样才能得到像 10:15 这样的 x 刻度来代替 (10, 15) 以及如何设法添加像 9:15, 9:30... 这样丢失的 x 刻度以获得完整的时间线?
解决方案
您可以resample
将TIME
列间隔为 15 分钟并计算行数。然后绘制一个常规条形图。
df = pd.DataFrame({'TIME': pd.to_datetime('2019-01-01') + pd.to_timedelta(pd.np.random.rand(100) * 3, unit='h')})
df = df[df.TIME.dt.minute > 15] # make gap
ax = df.resample('15T', on='TIME').count().plot.bar(rot=0)
ticklabels = [x.get_text()[-8:-3] for x in ax.get_xticklabels()]
ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(ticklabels))
(有关格式化熊猫条形图的日期时间刻度标签的详细信息,请参阅此问题)
推荐阅读
- java - 我已经截取了用 netbeans 制作的表格。1130X800尺寸
- dpdk - 如何调试 DPDK 库以诊断分段错误?
- sqlite - 将 RDBMS (SQLite) 数据公开为 SPARQL 端点
- sql - 无法在 INSERT 语句中返回带有 SRC 密钥的输出
- css - 错误或意图 - 固定 CSS 渐变被裁剪为 50%
- android - 清单文件中提到时无法接收广播
- kubernetes - Fluxcd 没有应用任何带有 err="running kubectl: 错误: 无法识别 \"STDIN\": ..."
- python - Pandas 数据框:df.shape 抛出错误'TypeError:'tuple'对象不可调用'
- angular - Angular Http 服务单元测试不适用于 API 路径参数 - HttpTestingController expectOne
- bash - 如何在启用“pipefail”的脚本中使用“head”?