每隔10分钟在matplotlib中绘制时间

时间:2015-02-16 12:07:41

标签: python matplotlib

Result of the graph我是matplotlib的新手,我想请求你的帮助。

问题是我要绘制的图表给了我一年的间隔,我只需要每天每10分钟绘制一次。

截至目前,时间只有1个值,即12:00:00。但是你可以看到你在我目前的图表中无法知道:(你可以教我这些人怎么做。

#The time variable is an array filled populated with time strings like ['11:55:53', '11:55:54', '11:55:54' ... ]
#The bandwidth variable is just populated with integers
import matplotlib.pyplot as plt
from datetime import datetime
import time as t
import matplotlib.dates as dt
import numpy as np

def DownloadGraph(bandwidth, time, host) :
x = []
y = bandwidth
last = []
for this in range(len(time)):
    x.append(datetime.strptime(time[this], '%I:%M:%S').time())    
fig = plt.figure() #for modifying the graph details
rect = fig.patch
ax1 = fig.add_subplot(1,1,1, axisbg = 'yellow') 
ax1.plot(x,y, 'blue',linewidth=1.6) 

1 个答案:

答案 0 :(得分:0)

x轴的问题是所有值都相同。 由于matplotlib不知道你感兴趣的x值范围 - 它默认为+ - 2年。

只要您有多个唯一的x值,范围就会从最小值设置为最大值。 使用不同的x值,您发布的y值将如下所示:

import datetime
import matplotlib.pyplot as plt
y = [74, 102, 74, 102, 12]
x = [datetime.datetime(1900, 1, 1, 0, 0),
     datetime.datetime(1900, 1, 1, 5, 0),
     datetime.datetime(1900, 1, 1, 10, 0),
     datetime.datetime(1900, 1, 1, 15, 0),
     datetime.datetime(1900, 1, 2, 20, 0)]

plt.plot(x, y)
plt.show()

enter image description here

要实现每个索引增加10分钟,您可以通过以下方式调整for循环:

for idx, this_time in enumerate(time):
    x.append(datetime.strptime(this_time, '%I:%M:%S').time()) + datetime.timedelta(minutes=10*idx)