[英]Plotting Time Stamp of Specific Format in matplotlib in python
DD.MM_HH:MM:SS.mmmmmm in this format timestamps are given so how to process them in matplotlib in python DD.MM_HH:MM:SS.mmmmmm给出了这种格式的时间戳,所以如何在python的 matplotlib 中处理它们
I was trying to, but somehow it was only accepting arrays of floats.我试图这样做,但不知何故它只接受了浮点数的 arrays。 How can I get it to plot the time?我怎样才能得到它到 plot 的时间? Do I have to modify the format in any way?我必须以任何方式修改格式吗?
I tried with this我试过这个
dates = matplotlib.dates.date2num(list_of_datetimes) matplotlib.pyplot.plot_date(dates, values)
But getting the following error但是得到以下错误
AttributeError: 'numpy.str_' object has no attribute 'toordinal'
Please provide the format which I should follow to parse this type of timestamp?请提供我应该遵循的格式来解析这种类型的时间戳?
convert the strings in list_of_datetimes to datetime
first:首先将 list_of_datetimes 中的字符串转换为datetime
:
from datetime import datetime
import matplotlib
# some example data:
list_of_datetimes = ['05.07_15:39:52.855866', '05.07_15:49:52.855866', '05.07_15:59:52.855866']
values = [1, 2, 3]
# to datetime:
dt = [datetime.strptime(s, '%d.%m_%H:%M:%S.%f') for s in list_of_datetimes]
dates = matplotlib.dates.date2num(dt)
matplotlib.pyplot.plot_date(dates, values)
An alternative could be plotting a timedelta since start on the x-axis to avoid issues with microsecond handling in matplotlib
:另一种方法是在 x 轴上绘制自开始以来的时间增量,以避免matplotlib
中的微秒处理问题:
date = ["05.07_15:39:52.855866","05.07_15:39:52.855869","05.07_15:39:52.855988","05.07_15:39:52.855889"]
values = [1, 2, 3, 4]
dt = [datetime.strptime(s, '%d.%m_%H:%M:%S.%f') for s in date]
# to timedelta:
td = [(d-dt[0]).total_seconds() for d in dt]
matplotlib.pyplot.plot(td, values)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.