matplotlib 如何指定时间定位器的开始计时时间戳?

编程入门 行业动态 更新时间:2024-10-24 17:28:49
本文介绍了matplotlib 如何指定时间定位器的开始计时时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想要的很简单,我只想让定位器刻度在指定的时间戳记开始:伪代码:locator.set_start_ticking_at( datetime_dummy )到目前为止,我没有找到任何东西.

这是这个问题的代码部分:

axes[0].set_xlim(datetime_dummy) # datetime_dummy = '2015-12-25 05:34:00'将matplotlib.dates导入为matdatesseclocator = matdates.SecondLocator(间隔=20)minlocator = matdates.MinuteLocator(时间间隔= 1)hourlocator = matdates.HourLocator(interval = 12)seclocator.MAXTICKS = 40000minlocator.MAXTICKS = 40000hourlocator.MAXTICKS = 40000MajorFmt = matdates.DateFormatter('%Y-%m-%d, %H:%M:%S')minorFmt = matdates.DateFormatter('%H:%M:%S')axes [0] .xaxis.set_major_locator(minlocator)axes [0] .xaxis.set_major_formatter(majorFmt)plt.setp(axes [0] .xaxis.get_majorticklabels(),rotation = 90)轴[0].xaxis.set_minor_locator(seclocator)轴[0].xaxis.set_minor_formatter(minorFmt)plt.setp(axes [0] .xaxis.get_minorticklabels(),rotation = 90)# 其他代码#将无花果另存为图片

上面代码的x轴刻度将使我明白

如何告诉次要定位器与主要定位器对齐?如何告诉定位器从哪个时间戳开始计时?

我尝试过的方法: set_xlim 不能解决问题seclocator.tick_values(datetime_dummy, datetime_dummy1) 什么都不做

解决方案

不要使用 interval 关键字参数,而是使用

All I want is quite straight forward, I just want the locator ticks to start at a specified timestamp: peudo code: locator.set_start_ticking_at( datetime_dummy ) I have no luck finding anything so far.

Here is the portion of the code for this question:

axes[0].set_xlim(datetime_dummy) # datetime_dummy = '2015-12-25 05:34:00' import matplotlib.dates as matdates seclocator = matdates.SecondLocator(interval=20) minlocator = matdates.MinuteLocator(interval=1) hourlocator = matdates.HourLocator(interval=12) seclocator.MAXTICKS = 40000 minlocator.MAXTICKS = 40000 hourlocator.MAXTICKS = 40000 majorFmt = matdates.DateFormatter('%Y-%m-%d, %H:%M:%S') minorFmt = matdates.DateFormatter('%H:%M:%S') axes[0].xaxis.set_major_locator(minlocator) axes[0].xaxis.set_major_formatter(majorFmt) plt.setp(axes[0].xaxis.get_majorticklabels(), rotation=90 ) axes[0].xaxis.set_minor_locator(seclocator) axes[0].xaxis.set_minor_formatter(minorFmt) plt.setp(axes[0].xaxis.get_minorticklabels(), rotation=90 ) # other codes # save fig as a picture

The x axis ticks of above code will get me:

How do I tell the minor locator to align with the major locator? How do I tell the locators which timestamp to start ticking at?

what I have tried: set_xlim doesn't do the trick seclocator.tick_values(datetime_dummy, datetime_dummy1) doesn't do anything

解决方案

Instead of using the interval keyword parameter, use bysecond and byminute to specify exactly which seconds and minutes you with to mark. The bysecond and byminute parameters are used to construct a dateutil rrule. The rrule generates datetimes which match certain specified patterns (or, one might say, "rules").

For example, bysecond=[20, 40] limits the datetimes to those whose seconds equal 20 or 40. Thus, below, the minor tick marks only appear for datetimes whose soconds equal 20 or 40.

import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as matdates N = 100 fig, ax = plt.subplots() x = np.arange(N).astype('<i8').view('M8[s]').tolist() y = (np.random.random(N)-0.5).cumsum() ax.plot(x, y) seclocator = matdates.SecondLocator(bysecond=[20, 40]) minlocator = matdates.MinuteLocator(byminute=range(60)) # range(60) is the default seclocator.MAXTICKS = 40000 minlocator.MAXTICKS = 40000 majorFmt = matdates.DateFormatter('%Y-%m-%d, %H:%M:%S') minorFmt = matdates.DateFormatter('%H:%M:%S') ax.xaxis.set_major_locator(minlocator) ax.xaxis.set_major_formatter(majorFmt) plt.setp(ax.xaxis.get_majorticklabels(), rotation=90) ax.xaxis.set_minor_locator(seclocator) ax.xaxis.set_minor_formatter(minorFmt) plt.setp(ax.xaxis.get_minorticklabels(), rotation=90) plt.subplots_adjust(bottom=0.5) plt.show()

更多推荐

matplotlib 如何指定时间定位器的开始计时时间戳?

本文发布于:2023-10-27 22:52:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   定位器   matplotlib

发布评论

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

>www.elefans.com

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