Matplotlib小时分钟直方图

问题描述 投票:0回答:1
jupyter notebook 5.2.2
Python 3.6.4
pandas 0.22.0
matplotlib 2.2.2

嗨,我正在尝试根据使用Hive SQL从hadoop商店检索的小时和分钟日志数据,在jupyter笔记本中呈现和格式化直方图。

我的演示文稿有问题。我希望能够设置从00:00到23:59的轴,其中的分档从零开始到下一分钟结束。我想要半小时的刻度线。我只是看不出怎么做。

以下内容将回收2年数据,包含1440行和每分钟的事件总数。

%%sql -o jondat
select eventtime, count(1) as cnt
from logs.eventlogs
group by eventtime

数据存储为字符串,但是时间和分钟hh:mm,但它似乎是由笔记本自动转换为sysdate加时间戳,我一直在播放这种格式的数据和其他。

如果我剥掉了我得到的冒号

df.dtypes

eventtime int64
cnt int64

如果我使用像管道那样的假填料我会得到

eventtime object
cnt int64

如果我用冒号离开结肠,我得到

eventtime datetime64
cnt int64

这是我目前正在使用的。

...
2018-11-22 00:27:00 32140
2018-11-22 00:28:00 32119
2018-11-22 00:29:00 31726
...
2018-11-22 23:30:00 47989
2018-11-22 23:31:00 40019
2018-11-22 23:32:00 40962
...

然后我可以绘制数据

%%local

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import datetime as dt
import mateplotlib.dates as md

xtformat = md.DateFormatter('%H:%M')

plt.rcParams['figure.figsize'] = [15,10]
df = pd.DataFrame(jondat)

x=df['eventtime']
b=144
y=df['cnt']

fig, ax=plt.subplots()

ax.xaxis_date()

ax.hist(x,b,weights=y)
ax.xaxis.set_major_formatter(xtformat)

plt.show(ax)

目前我的轴在数据之前和之后很好地开始,并且箱子在分钟上居中,如果我改变箱子的数量则更加痛苦。我看不到在哪里停止从字符串到日期时间的自动转换,我不确定是否需要以获得我想要的结果。

这是关于格式化我的事件时间和设置轴还是我可以轻松地设置轴而不管数据类型如何。理想情况下,标记的刻度将是用户友好的

This is the chart I get with 144 bins. As some of the log records are manual the 1440 bin chart is "hairy" due to the tendency for the manual records being rounded. One of the things I am experimenting with is different bin counts.

python pandas matplotlib hive jupyter-notebook
1个回答
0
投票

感谢https://stackoverflow.com/users/4124317/importanceofbeingernest,他给了我足够的线索来找到答案。

%%local

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import datetime as dt
import mateplotlib.dates as md

plt.rcParams['figure.figsize'] = [15,10]
df = pd.DataFrame(jondat)

xtformat = md.DateFormatter('%H:%M')
xtinter = md.MinuteLocator(byminute=[0], interval=1)
xtmin = md.MinuteLocator(byminute=[30], interval=1)


x=df['eventtime']
b=144
y=df[cnt']

fig, ax=plt.subplots()

ld=min(df['eventtime'])
hd=max(df['eventtime'])

ax.xaxis_date()

ax.hist(x,b,weights=y)
ax.xaxis.set_major_formatter(xtformat)
ax.xaxis.set_major_locator(xtinter)
ax.xaxis.set_minor_locator(stmin)
ax.set_xlim([ld,hd])

plt.show(ax);

这使我可以整齐地绘制图表并使用bin设置进行播放,以查看它对曲线的影响,以及在仪表板上的显示,以及帮助考虑分类到时间段以便按时间分析偶数类型。

© www.soinside.com 2019 - 2024. All rights reserved.