Python 在 matplotlib 中绘制 unix 时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4090383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:11:37  来源:igfitidea点击:

plotting unix timestamps in matplotlib

pythonmatplotlib

提问by Matt

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.

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

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?

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

采纳答案by unutbu

It is possible to call plt.plot(dates,values)with datesbeing a list of datetime.datetimeobjects. 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.

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

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.datetimeobjects to plt.plot, the xticks automatically chosen by matplotlib seems to always have seconds equal to zero. For example, if you run

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

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.

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

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.

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

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

附注。“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()

alt text

替代文字