在 matplotlib 中绘制 unix 时间戳 [英] plotting unix timestamps in matplotlib

查看:37
本文介绍了在 matplotlib 中绘制 unix 时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 python 的 matplotlib 模块制作一个通用的值-vs-时间图.我的时间是在 unix 时间,但我希望它们以可读的格式显示在绘图的 x 轴上.

I'd like to make a generic value -vs- time plot with python's matplotlib module. My times are in unix time but I'd like them to show up in a readable format on the plot's x-axis.

我已阅读有关使用日期时间对象进行绘图的答案,但此方法似乎删除了小时/分钟/秒信息并将时间戳添加到全天.有没有办法生成这些图并显示更精细的标签?

I have read answers about plotting with datetime objects but this method seems to remove hour/min/sec information and rails timestamps to the full day. Is there a way to generate these plots and show more granular labels?

推荐答案

可以使用 dates 作为列表来调用 plt.plot(dates,values)datetime.datetime 对象.该图将包含类似 '%Y-%m-%d' 格式的 xticks,当您放大时,会自动更改为显示小时、分钟、秒的格式.

It is possible to call plt.plot(dates,values) with dates being a list of datetime.datetime objects. The plot will include xticks in a format like '%Y-%m-%d' and as you zoom in, automatically change to one that shows hours, minutes, seconds.

但是,听起来您希望获得更多控制权.也许它没有按照您希望的比例显示小时、分钟、秒.

However, it sounds like you desire more control than this. Perhaps it is not showing the hours, minutes, seconds at the scale you wish.

在这种情况下,您可以设置自己的日期格式化程序:

In that case, you can set up your own date formatter:

ax=plt.gca()
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)

不幸的是,如果您将 datetime.datetime 对象传递给 plt.plot,matplotlib 自动选择的 xticks 似乎总是有等于 0 的秒数.例如,如果你运行

Unfortunately, if you pass datetime.datetime objects to plt.plot, the xticks automatically chosen by matplotlib seems to always have seconds equal to zero. For example, if you run

import matplotlib.pyplot as plt
import matplotlib.dates as md
import numpy as np
import datetime as dt
import time

n=20
duration=1000
now=time.mktime(time.localtime())
timestamps=np.linspace(now,now+duration,n)
dates=[dt.datetime.fromtimestamp(ts) for ts in timestamps]
values=np.sin((timestamps-now)/duration*2*np.pi)
plt.subplots_adjust(bottom=0.2)
plt.xticks( rotation=25 )
ax=plt.gca()
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
plt.plot(dates,values)
plt.show()

然后你会得到格式很好的日期,但所有的 xtick 秒都是零.

then you get nicely formatted dates, but all the xtick seconds are zero.

那么有什么解决办法呢?

So what's the solution?

如果您自己转换时间戳 --> datetime.datetime 对象 --> matplotlib datenums,并将 datenums 传递给 plt.plot,那么秒数将被保留.

If you convert your timestamps --> datetime.datetime objects --> matplotlib datenums yourself, and pass the datenums to plt.plot, then the seconds are preserved.

附注.matplotlib datenum"是指 matplotlib.dates.date2num 返回的数字类型.

PS. By "matplotlib datenum" I mean the kind of number returned by matplotlib.dates.date2num.

import matplotlib.pyplot as plt
import matplotlib.dates as md
import numpy as np
import datetime as dt
import time

n=20
duration=1000
now=time.mktime(time.localtime())
timestamps=np.linspace(now,now+duration,n)
dates=[dt.datetime.fromtimestamp(ts) for ts in timestamps]
datenums=md.date2num(dates)
values=np.sin((timestamps-now)/duration*2*np.pi)
plt.subplots_adjust(bottom=0.2)
plt.xticks( rotation=25 )
ax=plt.gca()
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
plt.plot(datenums,values)
plt.show()

这篇关于在 matplotlib 中绘制 unix 时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆