Matplotlib中x轴上的时间格式

艾哈迈德

x轴上的时间必须以3秒的时间间隔表示,但是数据中的时间增量是2秒。因此,我尝试按照代码中的说明进行操作,但是我得到的时间是在09:56:09之前,尽管时间是在09:56:22之前。

请注意,时间应从09:56:00开始

如何完全显示x轴上的时间?

码:

import matplotlib.pyplot as plt
import time
from datetime import datetime, timedelta
import numpy as np
import matplotlib.dates as mdates

SO=[100,300,600,800,850,310,560,790,500,810,790,490];the_date_0=['01-02-2018 09:56:00.0000','01-02-2018 09:56:02.0000',
                                                               '01-02-2018 09:56:04.0000','01-02-2018 09:56:06.0000',
                                                               '01-02-2018 09:56:08.0000','01-02-2018 09:56:10.0000',
                                                               '01-02-2018 09:56:12.0000','01-02-2018 09:56:14.0000',
                                                               '01-02-2018 09:56:16.0000','01-02-2018 09:56:18.0000',
                                                               '01-02-2018 09:56:20.0000','01-02-2018 09:56:22.0000']

the_date=[]
Date_axis=[]

for i in the_date_0:
    the_date.append(time.mktime(time.strptime(i, "%d-%m-%Y %H:%M:%S.0000")))

for i1 in range(0,len(the_date),3):
    time1=datetime.fromtimestamp(time.mktime(time.strptime("01-02-2018 09:56:00.0000", "%d-%m-%Y %H:%M:%S.0000")))+timedelta(seconds=i1)
    Date_axis.append(time.mktime(time1.timetuple()))

fig = plt.figure()
dateconv=np.vectorize(datetime.fromtimestamp)
Date_F=dateconv(the_date)
Date_axis_ok=dateconv(Date_axis)
ax1 = plt.subplot2grid((1,1), (0,0))
ax1.plot_date(Date_F,SO,'r-',label='the values')
ax1.set_yticks(range(0,1000,100))
ax1.set_xticks(Date_axis_ok)
ax1.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S"))
for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(45)
ax1.grid(True)
plt.legend()
plt.show()

图形 在此处输入图片说明

问题出for loop在中len(the_date)在您的代码中添加此部分:

from datetime import datetime, timedelta, date
duration = datetime.combine(date.min, datetime.strptime(the_date_0[-1], "%d-%m-%Y %H:%M:%S.0000").time()) - datetime.combine(date.min, datetime.strptime(the_date_0[0], "%d-%m-%Y %H:%M:%S.0000").time())
seconds = int(duration.total_seconds())

并以这种方式修改循环:

for i1 in range(0,seconds,3):

输出: 在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

matplotlib中的直方图,x轴上的时间

在Pandas-Matplotlib中设置X轴的日期时间格式

调整 x 轴上的时间戳 - Matplotlib

使用matplotlib在x轴上绘制时间

如何在matplotlib中以'%H:%M'格式在y轴上绘制时间?

如何使用Pandas / Matplotlib在X轴上绘制日期,在Y轴上绘制时间,并以HH:MM格式作为刻度标签绘制当前时间?

在matplotlib中在x轴上绘制日期

在Excel中在X轴上绘制时间

如何在matplotlib中的x轴上使用正确的日期时间索引正确绘制回归输出?

如何在matplotlib中的x轴上显示日期和时间

在matplotlib中更改日期时间轴的格式

在Matplotlib图的x轴上获取日期格式

x轴上的Nivo线格式化时间刻度

如何在折线图Python Matplotlib中为X轴设置所需的时间间隔和所需的格式?

在matplotlib中编辑X轴刻度标签的日期格式

在matplotlib中编辑X轴刻度标签的日期格式

gnuplot中x轴的日期和时间格式

时间序列图:在R中更改x轴格式

时间序列ggplot中x轴上的时间戳

在matplotlib中的日期时间轴上绘制矩形?

在Matplotlib中更改X轴日期时间间隔

时间序列季节性分解中的小刻度轴和主刻度轴matplotlib格式

matplotlib中的特定轴格式

从txt.file中读取时间戳,并使用matplotlib(Web服务器)在X轴上绘制它们

在matplotlib的x轴上断开//

Matplotlib datetime在x轴上

gnuplot-将x轴上的unix时间戳格式化为日期-时间

如何更改 matplotlib 中 x 轴上值的显示?

更改matplotlib中x或y轴上的“刻度频率”?