gpt4 book ai didi

每分钟事件的python matplot

转载 作者:行者123 更新时间:2023-12-04 19:17:08 24 4
gpt4 key购买 nike

我正在尝试使用 matplot 来绘制由不同输入生成的特定分钟内的事件数。

与下面的屏幕剪辑大致相似,但仅显示了一个简单的 24 小时时段。



我的部分问题是我不知道如何处理不完整的 Y 轴数据。例如,如果我只有 2 个事件,一个在 04:04 (y=5),一个在 13:22 (y=1) 请问如何在图表上显示完整的 24 小时?

我一直在尝试堆叠 histt​​ype=step,但我没有取得任何进展,并且在 http://matplotlib.org/gallery.html 中找不到任何示例

如果有更好的绘图仪用于这种输出,我也很乐意尝试。

谢谢

编辑:添加不是我想要的示例。
需要它:(1)。在 x 轴 (2) 上使用时间。使用事件的时间 (3)。从 0 (4) 开始的条形图。中间没有那条线(5)。稍后添加不同颜色的不同事件

import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')
import matplotlib.pyplot as plt

a = [1,2,5,6,9,11,15,17,18]
plt.hlines(1,0,24) # Draw a horizontal line
plt.eventplot(a, orientation='horizontal', colors='b')

plt.savefig('foo.png', bbox_inches='tight')

not quite right

最佳答案

我认为您想使用 plt.xlim 以分钟为单位设置 24 小时周期。它沿 xaxis 创建一个从 0-1440 的索引。然后,您可以附加高度等于您的 y 值的条形,并根据索引定位。

plt.figure(figsize=(29, 9))
hours = 24
mins = 60
xlabels = ['%02d:%02d' % (divmod(i, 60)) for i in range(0, mins * hours, 10)]
plt.xlim(0, hours * mins)
plt.ylim(0,6)
xdata_org = ['04:04', '13:22']
ydata = [5, 1]
def get_min(time):
l = time.split(':')
return int(l[0]) * 60 + int(l[1])
xdata = [get_min(i) for i in xdata_org]
plt.bar(xdata, ydata, width=1)
plt.xticks(range(0, hours * mins, 10), xlabels, rotation='vertical', fontsize=9)
plt.subplots_adjust(left=0.05, right=0.95, bottom=0.3)

关于每分钟事件的python matplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34302298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com