python - 如何在 y 轴上绘制日期,在 x 轴上绘制小时?

标签 python python-3.x pandas matplotlib plot

enter image description here

你好!

我有以下离散列表: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 - 如何在 y 轴上绘制日期,在 x 轴上绘制小时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54911354/

相关文章:

python - 添加条件值位于某些其他列中任意位置的列

javascript - 使用 Selenium 在 Python 中执行 JavaScript

python - 移动 QSlider 到鼠标点击位置

python - Pandas:astype错误字符串到 float (无法将字符串转换为 float : '7,50')

python - 转储 Python 字典时出现错误,表示它是 Pandas DataFrame

Python 文档首页

python - 为什么在执行 python 脚本时会出现 No such file or directory 错误?

Python-从文件夹目录创建字典

python - 为什么 PyInstaller 没有检测到请求模块?

python - Pandas between_time 等效于 Dask DataFrame