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

tmb3ates  于 2023-06-06  发布在  其他
关注(0)|答案(2)|浏览(110)

我想要的是非常直接的,我只想定位器滴答声在指定的时间戳开始:
peudo code: locator.set_start_ticking_at( datetime_dummy )
目前为止我还没有找到任何线索。
下面是这个问题的代码部分:

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

上面代码的x轴刻度将得到我:

如何让次定位器与主定位器对齐?
如何告诉定位器在哪个时间戳开始计时?
我尝试过的:
set_xlim不起作用
seclocator.tick_values(datetime_dummy, datetime_dummy1)不执行任何操作

jtjikinw

jtjikinw1#

不使用interval关键字参数,而是使用bysecondbyminute来指定要标记的秒和分钟。bysecondbyminute参数用于构造dateutil rrule。rrule生成与某些指定模式(或者可以说是“规则”)匹配的日期时间。
例如,bysecond=[20, 40]将日期时间限制为second s等于20或40的日期时间。因此,下面的小刻度线只出现在soconds等于20或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()

vngu2lb8

vngu2lb82#

@unutbu:非常感谢:我一直在到处寻找一个相关问题的答案!
@eliu:我已经改编了unutbu的优秀答案,以演示如何定义列表(创建不同的“dateutil”规则),这些规则可以给予您完全控制显示哪些x-ticks。尝试依次取消注解下面的每个示例,并尝试使用这些值来查看效果。希望这能帮上忙。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.Series(np.random.randn(len(idx)),  index = idx)
fig, ax = plt.subplots()

# Choose which major hour ticks are displayed by creating a 'dateutil' rule e.g.:

# Only use the hours in an explicit list:
# hourlocator = mdates.HourLocator(byhour=[6,12,8])

# Use the hours in a range defined by: Start, Stop, Step:
# hourlocator = mdates.HourLocator(byhour=range(8,15,2))

# Use every 3rd hour:
# hourlocator = mdates.HourLocator(interval = 3)

# Set the format of the major x-ticks:
majorFmt = mdates.DateFormatter('%H:%M')  

ax.xaxis.set_major_locator(hourlocator)
ax.xaxis.set_major_formatter(majorFmt)

#... and ditto to set minor_locators and minor_formatters for minor x-ticks if needed as well)

ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)

fig.autofmt_xdate() # optional: makes 30 deg tilt on tick labels

plt.show()

相关问题