我试图用温度对时间绘制温度值,格式为HH:MM.我能够将xticks设置为每15分钟重复一次,但第一次打勾是第一次(例如04:40).
有没有办法在每小时和同时的四分之一小时(04:45,05:00,05:15等)上移动刻度?我目前的代码如下:
import matplotlib.pyplot as plt
import matplotlib.dates as md
import datetime as dt
## Dummy times and temperatures
time = [dt.datetime(2017,2,15,4,40),dt.datetime(2017,2,15,4,46),dt.datetime(2017,2,15,4,52),dt.datetime(2017,2,15,4,58),dt.datetime(2017,2,15,5,4),dt.datetime(2017,2,15,5,10)]
temp = [7, 8, 9, 10, 11, 12]
## Plot the data
figtemp, ax = plt.subplots(1, 1)
ax.plot(time, temp)
## Set time format and the interval of ticks (every 15 minutes)
xformatter = md.DateFormatter('%H:%M')
xlocator = md.MinuteLocator(interval = 15)
## Set xtick labels to appear every 15 minutes
ax.xaxis.set_major_locator(xlocator)
## Format xtick labels as HH:MM
plt.gcf().axes[0].xaxis.set_major_formatter(xformatter)
Run Code Online (Sandbox Code Playgroud)