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

查看:58
本文介绍了在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?

推荐答案

可以调用plt.plot(dates,values),其中datesdatetime.datetime对象的列表.该图将包含格式为'%Y-%m-%d'的xticks,并且在放大时会自动更改为显示小时,分钟,秒的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似乎总是具有等于零的秒数.例如,如果您运行

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.

那是什么解决方案?

如果您自己转换时间戳-> 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.

PS. "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天全站免登陆