[英]how to set the xaxis in time interval, say 15min using matplotlib
How to set the xaxis in time interval 15minutes as: 如何以15分钟的时间间隔将xaxis设置为:
'08:00', '08:15', '08:30', '08:45', '09:00', '09:15', '09:30', '09:45', '10:00','10:15', '10:30', '10:45','11:00'
So each one is a label in xaxis. 因此,每个都是xaxis中的标签。
data is as follows: 数据如下:
data = [[0,1],[08:18,09:16],[10:18,10:52]]
x1 = data[:][1]
x2 = data[:][2]
y = data[:][0]
I want to use plot function to plot with data as above: 我想使用plot函数以上述方式绘制数据:
plt.plot(data[:][1], y,
data[:][2], y)
After plotting, there should have two lines on the map. 绘制后,地图上应该有两条线。 And the labels are in time interval of 15min.
标签间隔为15分钟。
The given data
is not valid in Python. 给定的
data
在Python中无效。 It should be like [["0","1"],["08:18","09:16"]]
. 它应该像
[["0","1"],["08:18","09:16"]]
。
Write a function to translate the time intervals into real number. 编写一个将时间间隔转换为实数的函数。 (For understandablity to you, the code is not that pythonic.)
(为了便于您理解,代码不是那种pythonic。)
def time_to_num(time_str): time_str = time_str.split(':') #check if time_str is valid here, then: num = 0 num = int(time_str[0]) if len(time_str) == 2: num += int(time_str[1])/60.0 return num
Make it like x1 = [time_to_num(item[1]) for item in data]
使它像
x1 = [time_to_num(item[1]) for item in data]
Set the X axis lable as time interval with something like: 将X轴标签设置为时间间隔,例如:
#fig = matplotlib.plot... fig.set_xticks([0, 0.25, 0.5, 0.75, 1, 1.25]) fig.set_xticklabels(['0:00', '0:15', '0:30', '0:45', '1:00', '1:15'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.