简体   繁体   English

隐藏特定的X轴刻度标签

[英]Hide specific x-axis tick label

I'm interested in having more space added between the last scatter point and the Y2 (right) spine, rather than having the point immediately adjacent to the spine (see attached PNG 我有兴趣在最后一个散点和Y2(右)脊之间添加更多空间,而不是使该点紧邻脊(请参阅附件PNG) 在此处输入图片说明 ). )。

I can add the desired space by adding a year to the plt.ticks command but then "2017" is displayed which I don't want. 我可以通过在plt.ticks命令中添加一年来添加所需的空间,但随后会显示“ 2017”,这是我不需要的。

Is there a way to either (a) add space between the last scatter point and the spine using an existing command or (b) use the approach I attempted and hide or make the last label match the background color, or (c) since I'm new to matplotlib and I'm not familiar with all of the terminology, direct me to an existing link? 有没有一种方法可以(a)使用现有命令在最后一个散点和书脊之间添加空间,或者(b)使用我尝试的方法来隐藏或使最后一个标签与背景颜色匹配,或者(c)自从我是matplotlib的新手,我对所有术语都不熟悉,可以直接转到现有链接吗?

Thanks in advance. 提前致谢。

plt.xticks(["1975-01-01", "1980-01-01", "1985-01-01", "1990-01-01", "1995-01-01", "2000-01-01", "2005-01-01", "2010-01-01", "2015-01-01", "2017-01-01"])

I find that using datetime objects gives ticks in the right place without modifying plt.ticks directly: 我发现使用datetime对象可以在正确的位置提供刻度,而无需直接修改plt.ticks

import matplotlib.pyplot as plt
from datetime import datetime

# A few example data points
dates = ['1979-04-3', '1990-05-06', '2000-12-12', '2015-04-20']
dates = [datetime.strptime(date, '%Y-%m-%d') for date in dates]
y = [200, 315, 401, 513]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(dates, y, 'd')
ax.set_ylim(0,800)

# set the x-axis limits explicitly, using datetime objects
xmin = datetime.strptime('1975-01-01', '%Y-%m-%d')
xmax = datetime.strptime('2017-01-01', '%Y-%m-%d')
ax.set_xlim(xmin, xmax)

plt.show()

If you do need finer control over the x-ticks, you could set them to the years 1975-2015 (inclusive) in 5-year intervals with: 如果确实需要更好地控制x刻度,可以将它们设置为1975-2015年(包括5年),间隔为5年,其中:

ax.set_xticks([datetime.strptime(str(y), '%Y') for y in range(1975,2020,5)])

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM