简体   繁体   English

熊猫日期时间图轴上的自定义刻度位置

[英]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.

相关问题 如何 plot 条上的垂直线 plot 与日期时间 label 只有谨慎的 x 轴刻度位置? - How to plot a vertical line on a bar plot with a datetime label with only discreet x-axis tick locations? 如何为从pandas DataFrame创建的条形图设置x轴刻度位置? - How can I set the x-axis tick locations for a bar plot created from a pandas DataFrame? x轴上带有时间戳的熊猫图-刻度单位? - pandas plot with timestamps on x axis - tick units? 如何在熊猫图中旋转 x 轴刻度标签 - How to rotate x-axis tick labels in a pandas plot 将附加子图的 x 轴刻度位置与 FacetGrid 对象的 x 轴刻度位置对齐 - Align x-axis tick locations of additional subplot with x-axis tick locations of FacetGrid object 熊猫/ matplotlib日期时间刻度标签 - pandas/matplotlib datetime tick labels 将x轴刻度标签格式化为接缝,例如默认的熊猫图 - Format x-axis tick labels to seams like the default pandas plot 如何使用 pandas plot 去除多余的 x 轴日期刻度线? - How to get rid of superfluous x-axis date tick marks with pandas plot? pandas.plot() x 轴刻度频率——如何显示更多刻度? - pandas .plot() x-axis tick frequency -- how can I show more ticks? 如何使用 Pandas/Matplotlib 将 plot X 轴上的日期、Y 轴上的时间和 HH:MM 格式的当前时间作为刻度标签? - How to plot Date in X Axis, Time in Y axis with Pandas/Matplotlib and present time in HH:MM format as tick labels?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM