[英]Custom tick locations on Pandas datetime plot axis
I have a plot in Pandas with dates on the x-axis and some number on the y-axis. 我在Pandas中有一个图,在x轴上有日期,在y轴上有一些数字。 I want to set the x-axis ticks on specific dates.
我想在特定日期设置X轴刻度。 I can't figure out how.
我不知道怎么办。 For instance, given this code:
例如,给出以下代码:
import pandas as pd
import numpy as np
x = pd.date_range('1946-06-07', '1967-03-15', freq='M')
y = np.random.random(len(x))
d = pd.DataFrame({'month': x, 'value': y})
d.plot()
How do I set the x-ticks to some specified set of ticks, say 1949, 1950, 1954 and 1960, instead of the automatic regularly spaced ticks? 如何将x刻度设置为某些指定的刻度,例如1949、1950、1954和1960,而不是自动按规则间隔的刻度? If only regularly spaced ticks are possible, how can I specify the first of them so that for instance it starts at 1949 and then goes on in 5 year intervals?
如果只有规则间隔的刻度是可行的,我如何指定它们中的第一个,例如从1949年开始,然后每5年间隔一次?
For some reason, I did not consider the most obvious solution. 由于某种原因,我没有考虑最明显的解决方案。 This works like a charm:
这就像一个魅力:
locations = ['1948-7-7', '1952-6-25', '1956-6-13', '1959-3-12', '1963-5-15']
labels = locations
plt.xticks(locations, labels)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.