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

时间:2016-05-13 21:33:59

标签: python matplotlib

我想要的只是非常简单,我只想让定位器滴答在指定的时间戳开始:
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
到目前为止我找不到任何运气。

以下是此问题的代码部分:

set_xlim

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

enter image description here

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

我试过的是什么:
seclocator.tick_values(datetime_dummy, datetime_dummy1)没有做到这一点 JobId没有做任何事情

2 个答案:

答案 0 :(得分:4)

不使用interval关键字参数,而是使用bysecondbyminute来准确指定要标记的秒数和分钟数。 bysecondbyminute参数用于构建dateutil rrulerrule生成与某些指定模式匹配的日期时间(或者,可能会说,“规则”)。

例如,bysecond=[20, 40]将日期时间限制为second个 等于20或40.因此,在下面,次要刻度标记仅出现在日期时间 他们的社会等于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()

enter image description here

答案 1 :(得分:0)

@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()