Matplotlib基于时分的直方图

2024-04-26 04:20:42 发布

您现在位置:Python中文网/ 问答频道 /正文

jupyter notebook 5.2.2
Python 3.6.4
pandas 0.22.0
matplotlib 2.2.2

嗨,我正在尝试在jupyter笔记本中显示和格式化一个柱状图,它基于使用hivesql从hadoop存储中检索到的小时和分钟日志数据。在

我的演讲有问题。我想把轴从00:00设置到23:59,箱子从零开始到下一分钟结束。我想每半小时打个记号。我就是不知道怎么做。在

以下是两年的数据,共1440行,每分钟的事件总数。在

^{pr2}$

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

如果我去掉冒号

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.


Tags: the数据importdfmatplotlibisas时间
1条回答
网友
1楼 · 发布于 2024-04-26 04:20:42

感谢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设置,以查看它对仪表板上的显示曲线的影响程度,并帮助考虑将其分类到时间段中,以便按时间对偶数类型进行分析。在

相关问题 更多 >

    热门问题