问题描述
我有一个格式为(HH:MM:SS.mmmmmm)的时间戳阵列和另一个浮点数数组,每个浮点数对应于timestamp数组中的一个值。
我可以在x轴上绘制时间并使用Matplotlib绘制y轴上的数字吗?
我正在尝试,但不知何故它只接受数组的花车。我怎样才能得到它的时间?我必须以任何方式修改格式吗?您必须先将时间戳转换为Python datetime
对象(使用 datetime.strptime
)。然后使用 date2num
将日期转换为matplotlib格式。
使用:
日期= matplotlib.dates.date2num(list_of_datetimes)
matplotlib.pyplot.plot_date(日期,值)
I have an array of timestamps in the format (HH:MM:SS.mmmmmm) and another array of floating point numbers, each corresponding to a value in the timestamp array.
Can I plot time on the x axis and the numbers on the y-axis using Matplotlib?
I was trying to, but somehow it was only accepting arrays of floats. How can I get it to plot the time? Do I have to modify the format in any way?
You must first convert your timestamps to Python datetime
objects (use datetime.strptime
). Then use date2num
to convert the dates to matplotlib format.
Plot the dates and values using plot_date
:
dates = matplotlib.dates.date2num(list_of_datetimes)
matplotlib.pyplot.plot_date(dates, values)
这篇关于使用Matplotlib在Python中绘制时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!