Python 使用 Matplotlib 绘制时间戳(小时/分钟/秒)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17709823/
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-19 08:58:49  来源:igfitidea点击:

Plotting timestamps (hour/minute/seconds) with Matplotlib

pythonmatplotlibtimestamp

提问by user1048858

I want to plot some timestamps (Year-month-day Hour-Minute-Second format). I am using the following code, however it doesn't show any hour-minute-second information, it shows them as 00-00-00. I double checked my date array, and as you can see from the snippet below, they are not zero.

我想绘制一些时间戳(年-月-日时-分-秒格式)。我正在使用以下代码,但是它没有显示任何时-分-秒信息,而是将它们显示为 00-00-00。我仔细检查了我的日期数组,正如您从下面的代码段中看到的,它们不为零。

Do you have any idea about why I am getting 00-00-00's?enter image description here

你知道我为什么会收到 00-00-00 吗?在此处输入图片说明

import matplotlib.pyplot as plt
import matplotlib.dates as md
import dateutil

dates = [dateutil.parser.parse(s) for s in datestrings]
# datestrings = ['2012-02-21 11:28:17.980000', '2012-02-21 12:15:32.453000', '2012-02-21 23:26:23.734000', '2012-02-26 17:42:15.804000']
plt.subplots_adjust(bottom=0.2)
plt.xticks( rotation= 80 )
ax=plt.gca()
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
plt.plot(dates[0:10],plt_data[0:10], "o-")
plt.show()

采纳答案by seth

Try zooming in on your graph, you will see the datetimes expand as your x axis scale changes.

尝试放大图形,您会看到日期时间随着 x 轴比例的变化而扩展。

plotting unix timestamps in matplotlib

在 matplotlib 中绘制 unix 时间戳

I had a similarly annoying problem when trying to plot heatmaps of positive selection on chromosomes. If I zoomed out too far things would disappear entirely!

在尝试绘制染色体上正选择的热图时,我遇到了类似的烦人问题。如果我缩小得太远,事情就会完全消失!

edit: This code plots your dates exactly as you give them, but doesn't add ticks in between.

编辑:此代码完全按照您提供的日期绘制您的日期,但不会在两者之间添加刻度。

import matplotlib.pyplot as plt
import matplotlib.dates as md
import dateutil

datestrings = ['2012-02-21 11:28:17.980000', '2012-02-21 12:15:32.453000', '2012-02-21 23:26:23.734000', '2012-02-26 17:42:15.804000']
dates = [dateutil.parser.parse(s) for s in datestrings]

plt_data = range(5,9)
plt.subplots_adjust(bottom=0.2)
plt.xticks( rotation=25 )

ax=plt.gca()
ax.set_xticks(dates)

xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
plt.plot(dates,plt_data, "o-")
plt.show()

回答by mintchip36

I can tell you why it shows the 00:00:00. It's because that's the start time of that particular day. For example, one tick is at 2012-02-22 00:00:00 (12 midnight of 2012-02-22) and another is at 2012-02-23 00:00:00 (12 midnight of 2012-02-23).

我可以告诉你为什么它显示 00:00:00。因为那是那一天的开始时间。例如,一个刻度是在 2012-02-22 00:00:00(2012-02-22 午夜 12 点),另一个是在 2012-02-23 00:00:00(2012-02-23 午夜 12 点) )。

Ticks for the timestamps in between these two times are not shown.

未显示这两个时间之间的时间戳记。

I myself am trying to figure out how to show ticks for in between these times.

我自己正试图弄清楚如何在这些时间之间显示刻度。