在matplotlib中绘制unix时间戳(plotting unix timestamps in matplotlib)

系统教程 行业动态 更新时间:2024-06-14 16:59:22
在matplotlib中绘制unix时间戳(plotting unix timestamps in matplotlib)

我想用python的matplotlib模块制作一个通用的值 - 时间图。 我的时间在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) , dates是datetime.datetime对象的列表。 情节将包括像'%Y-%m-%d'这样的格式的xticks,并且在您放大时会自动更改为显示小时,分钟和秒的格式。

但是,这听起来像你想要更多的控制比这个。 也许它不会显示您希望的规模的小时,分​​钟,秒。

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

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

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

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秒都是零。

那么解决方案是什么?

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

PS。 “matplotlib datenum”是指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()

替代文字

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)

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()

alt text

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

So what's the solution?

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

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()

alt text

更多推荐

本文发布于:2023-04-16 19:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/79cea11df34d898c6513b6486422758d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   unix   matplotlib   plotting   timestamps

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!