Plotting timestamps (hour/minute/seconds) with Matplotlib(使用 Matplotlib 绘制时间戳(小时/分钟/秒))
问题描述
我想绘制一些时间戳(年-月-日时-分-秒格式).我正在使用以下代码,但它没有显示任何小时-分钟-秒信息,它将它们显示为 00-00-00.我仔细检查了我的日期数组,从下面的代码片段可以看出,它们不为零.
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?
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()
推荐答案
尝试放大图表,您会看到日期时间随着 x 轴比例的变化而扩大.
Try zooming in on your graph, you will see the datetimes expand as your x axis scale changes.
在 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()
这篇关于使用 Matplotlib 绘制时间戳(小时/分钟/秒)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Matplotlib 绘制时间戳(小时/分钟/秒)


基础教程推荐
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 包装空间模型 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01