[英]matplotlib HourLocator steals my x labels
When I plot my half hourly time series, my axis labels are odd (like 16:33:12h or so...) When I use HourLocator to fix this (16:33h -> 16:00h), then my x label disappear completely.当我绘制我的半小时时间序列时,我的轴标签很奇怪(比如 16:33:12h 左右......)完全地。
My code is:我的代码是:
from datetime import date, timedelta, datetime, time
from matplotlib.dates import DayLocator, HourLocator
import matplotlib.pyplot as plt
start = time(0, 0, 0)
delta = timedelta(minutes=30)
times = []
for i in range(len(day_load)):
dt = datetime.combine(date.today(), time(0, 0)) + delta * i
times.append(dt.time())
load = [i/48 for i in range(48)]
fig, ax = plt.subplots()
ax.plot_date(times, load)
ax.xaxis.set_major_locator(HourLocator())
plt.show()
How can I achieve "even" labels (in a best practice way - I don't want to rewrite code for every other plot again).我怎样才能实现“偶数”标签(以最佳实践方式 - 我不想再次为每个其他情节重写代码)。 When I comment second last line, I get normal "odd" labels :(
当我评论倒数第二行时,我得到正常的“奇数”标签:(
Thanks for answers!感谢您的回答!
There are two main issues:主要有两个问题:
dt.time()
you should append dt
directly.dt
而不是dt.time()
。DateFormatter("%H:%M")
to show hours and minutes.DateFormatter("%H:%M")
来显示小时和分钟。 Complete code:完整代码:
from datetime import date, timedelta, datetime, time
from matplotlib.dates import DayLocator, HourLocator,DateFormatter
import matplotlib.pyplot as plt
start = time(0, 0, 0)
delta = timedelta(minutes=30)
times = []
n=48
for i in range(n):
# use complete datetime object, not only time
dt = datetime.combine(date.today(), time(0, 0)) + delta * i
times.append(dt)
load = [i/float(n) for i in range(n)]
fig, ax = plt.subplots()
ax.plot_date(times, load)
# set a locator, as well as a formatter
ax.xaxis.set_major_locator(HourLocator())
ax.xaxis.set_major_formatter(DateFormatter("%H:%M"))
#optionally rotate the labels and make more space for them
fig.autofmt_xdate()
plt.show()
This is an old question, but for anyone else facing this problem: you can leave the data types what they should be and use matplotlib.ticker.IndexLocator
to get the axis ticks located nicely.这是一个老问题,但对于其他面临此问题的人:您可以保留数据类型应该是什么,并使用
matplotlib.ticker.IndexLocator
来很好地定位轴刻度。
For example,例如,
locator = mpl.ticker.IndexLocator(base=2 * 60 * 60, offset=0)
ax.xaxis.set_major_locator(locator)
places ticks at every two full hours, ie it uses the total number of seconds since midnight, regardless of the length of the intervals in the data.每两个完整小时放置一次刻度,即它使用自午夜以来的总秒数,而不管数据中的间隔长度如何。
Your code doesn't run, because day_load
is undefined and I get other issues as well.您的代码没有运行,因为
day_load
未定义,我也遇到了其他问题。
Not an answer, but I think you're better of using pandas
.不是答案,但我认为您最好使用
pandas
。 It makes it easy to create a date_range
, and plotting is handled pretty well without adjustments.它可以轻松创建
date_range
,并且无需调整即可很好地处理绘图。
from scipy import stats
import pandas as pd
n = 20
index = pd.date_range(start = '2016-01-01', periods = n, freq='1H')
df = pd.DataFrame(index = index)
df["value"] = stats.norm().rvs(n)
df.plot()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.