python - 如何在 y 轴上绘制日期,在 x 轴上绘制小时数?
问题描述
你好 !
我有以下离散列表:found_time = ['2019-02-28 00:24:16', '2019-02-28 00:22:30', '2019-02-28 00:20:16', '2019 -02-28 00:19:25'、'2019-02-28 00:18:11'、'2019-02-27 23:09:45'、'2019-02-27 22:52:09'、 '2019-02-27 22:50:05', '2019-02-27 22:44:31', '2019-02-27 18:04:18', '2019-02-27 08:08:21 ', ... ... , '2019-02-01 22:21:10', '2019-02-01 00:21:10']
我想通过使用 matplotlib 或其他东西将点绘制为链接图像。
我设法通过使用来分隔日期和时间
data = panda.to_datetime(found_time, yearfirst=True)
x = data.time
y = data.date
我试过plt.xlim(["00:00:00", "23:59:59"])
了,但间隔是随机的。
问题:如何设置 y 轴 02-01 ~ 02-28 间隔 1 天,x 轴 00:00 ~ 24:00(或 23:59) 间隔 1 小时?
解决方案
您可以尝试先将日期时间列表转换为pandas.Series
,然后重新采样为小时。就像是:
import pandas as pd
import numpy as np
s = pd.Series(np.ones(len(found_time)), index=pd.DatetimeIndex(found_time))
s = s.resample('H').sum()
plt.scatter(s.index.hour, s.index.date, s=s, color='k')
# yticks
yticks = pd.date_range('2019-02-01', '2019-02-28', freq='D')
plt.yticks(yticks, [y.strftime('%m-%d') for y in yticks])
plt.ylim('2019-02-01', '2019-02-28')
# xticks
xticks = np.arange(24)
plt.xticks(xticks, ['{0:02d}:00'.format(x) for x in xticks], rotation=45, ha='right')
推荐阅读
- python - Mac 上的 SageMath 9.1:安装 numpy / pandas
- python - TypeError: unhashable type: 'list' 添加交互功能时
- nestjs - 我如何才能将下一个与嵌套集成?
- python-3.x - 使用 scipy 插值模块创建 .exe 文件
- ios - 如何获得两次之间的时差
- python - python:动态创建函数
- delete-row - 无法删除 R 中的特定行
- javascript - 如何对通过在javascript中连接两个不同数组创建的数组进行排序
- python - 计算字段未保存在 django 管理模型的数据库中
- html - 下拉菜单适用于 Chrome 而不是 Firefox