Matplotlib:从每小时开始每15分钟xticks [英] Matplotlib: xticks every 15 minutes, starting on the hour

查看:217
本文介绍了Matplotlib:从每小时开始每15分钟xticks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将温度值与时间相对应,时间格式为HH:MM.我可以将xticks设置为每15分钟重复一次,但第一次滴答是在第一次(例如04:40).

I am trying to plot values of temperature against time with the time formatted as HH:MM. I am able to set the xticks to recur every 15 minutes but the first tick is at the first time (e.g. 04:40).

是否有一种方法可以改变刻度线,使其发生在小时数和同时发生的四分之一小时(04:45、05:00、05:15等)上?我当前的代码如下:

Is there a way to shift the ticks to occur on the hour and on the concurrent quarter-hours (04:45, 05:00, 05:15, etc.)? My current code is as follows:

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)

推荐答案

您可以使用byminute参数告诉MinuteLocator仅使用分钟0,15,30,45.

You could tell the MinuteLocator to only use the minutes 0,15,30,45 using the byminute argument.

xlocator = md.MinuteLocator(byminute=[0,15,30,45], interval = 1)

这篇关于Matplotlib:从每小时开始每15分钟xticks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆