简体   繁体   English

Matplotlib:添加自定义刻度标签并删除y轴上的自定义刻度

[英]Matplotlib: add custom tick label and remove custom tick on y-axis

I would like to remove a tick and its label and instead add a new tick label in a different (but close) location without the tick itself. 我想删除一个刻度线及其标签,而是在没有刻度线本身的其他(但关闭)位置添加一个新的刻度线标签。 I am not sure how to do this. 我不确定该怎么做。 An example is given. 举一个例子。

import matplotlib.pyplot as plt
def main():
    fig = plt.figure()
    x = [1,2,3,4,5]
    y = x
    ax = fig.add_subplot(1,1,1)
    y_ticks = ax.yaxis.get_major_ticks()
    y_ticks[-1].label.set_visible(False)
    ax.set_yticks([3.7], minor=True)
    ax.set_yticklabels(["100"], minor=True)
    ax.plot(x,y)
    plt.show()
if __name__ == '__main__':
    main()

What is left for me here is to remove the ticks located at y=3.5, 3.7. 我剩下的是删除位于y = 3.5,3.7的刻度线。

Also, as you can see I accessed y_ticks[-1] to set its label to be invisible. 另外,如您所见,我访问了y_ticks [-1]以将其标签设置为不可见。 This was chosen arbitrarily and actually I would appreciate it if someone could explain why y_ticks[-1] chooses the tick located at y=3.5. 这是任意选择的,如果有人可以解释为什么y_ticks [-1]选择位于y = 3.5的刻度,我将不胜感激。

Thanks 谢谢

在此处输入图片说明

Edit: 编辑:

As was mentioned in the two answers I changed my code to: 如两个答案中所述,我将代码更改为:

import matplotlib.pyplot as plt

def main():
    fig = plt.figure()
    x = [1,2,3,4,5]
    y = x
    ax = fig.add_subplot(1,1,1)
    ax.plot(x,y)
    ax.set_yticks([3.7], minor=True)
    ax.set_yticklabels(["100"], minor=True)
    y_ticks = ax.yaxis.get_major_ticks()
    y_ticks[5].set_visible(False)
    y_ticks[6].label.set_visible(False)
    plt.show()
if __name__ == '__main__':
    main()

This yields the plot below and you can see that y_ticks[5] refers to the tick located at y=3.5 and y_ticks[6] to the tick located at y=4.0 - it skips the tick located at y=3.7 . 这产生了下面的图,您可以看到y_ticks[5]指向位于y=3.5的刻度线, y_ticks[6]指向位于y=4.0的刻度线-它跳过了y=3.7的刻度线。 So how can I access it and remove ONLY the tick? 那么我如何才能访问它并仅删除刻度线? The two suggestions only explain how to remove either both tick and label or only the label (without the tick) and I am looking for removing the tick and keeping the label. 这两个建议仅说明了如何同时删除刻度线和标签或仅删除标签(不带刻度线),我正在寻找删除刻度线并保留标签的方法。

在此处输入图片说明

Try y_ticks[-1].set_visible(False) instead of y_ticks[-1].label.set_visible(False) . 尝试使用y_ticks[-1].set_visible(False)代替y_ticks[-1].label.set_visible(False) It makes both the label and tick marker invisible. 它使标签和刻度标记都不可见。

edit: 编辑:

You can access the small tick at 3,7 using ax.yaxis.get_minor_ticks() like in the following code: 您可以使用ax.yaxis.get_minor_ticks()3,7处访问小刻度线,如以下代码所示:

import matplotlib.pyplot as plt

fig = plt.figure()
x = [1,2,3,4,5]
y = x
ax = fig.add_subplot(1,1,1)
ax.set_yticks([3.7], minor=True)
ax.set_yticklabels(["100"], minor=True)
ax.plot(x,y)

y_ticks = ax.yaxis.get_major_ticks()
y_ticks[5].set_visible(False)
y_minor_ticks = ax.yaxis.get_minor_ticks()
y_minor_ticks[0].label.set_visible(False)

plt.show()

If You want just the tick near 100 to disappear, and You want to keep the label, use: 如果您只希望100附近的刻度消失,并且想要保留标签,请使用:

ax.yaxis.get_minorticklines()[0].set_visible(False)

instead of : 代替 :

y_minor_ticks = ax.yaxis.get_minor_ticks()
y_minor_ticks[0].label.set_visible(False)

All those functions allowing accessing different tick parts are described in matplotlib.axis documentation . matplotlib.axis 文档中介绍了所有允许访问不同刻度部分的功能。

What happens is: You're getting the list of y_ticks after you've created an axes, but before you plot anything. 什么情况是:你得到y_ticks的列表中,您已经创建了一个轴 ,但你之前绘制任何东西。 So matplotlib sets up an axes with six ticks on both axes, from 0.0 to 1.0, and gives you the tick list. 因此,matplotlib设置了一个在两个轴上都带有六个刻度的轴(从0.0到1.0),并提供了刻度列表。

Then, when you plot, more ticks are added and the ticks in your list are updated with the new labels. 然后,在绘制时,将添加更多的刻度,并使用新标签更新列表中的刻度。 The last item in your y_ticks[] is now in the middle of the Y axis. y_ticks []中的最后一项现在位于Y轴的中间。

Solution: Plot first, then y_ticks = ax.yaxis.get_major_ticks() . 解决方案:首先绘制, 然后 y_ticks = ax.yaxis.get_major_ticks()

Ok, found an answer. 好,找到答案了。 The added y tick at y=3.7 was added to the minor axis rather than the major axis. y=3.7处添加的y刻度添加到短轴而不是长轴。 Therefore I accessed it by ax.tick_params(axis='y', which='minor', length=0) . 因此我通过ax.tick_params(axis='y', which='minor', length=0)访问它。 This however will set ALL minor ticks according to tick_params . 但是,这将根据tick_params设置所有次要滴答tick_params

import matplotlib.pyplot as plt

def main():
    fig = plt.figure()
    x = [1,2,3,4,5]
    y = x
    ax = fig.add_subplot(1,1,1)
    ax.plot(x,y)
    ax.set_yticks([3.7], minor=True)
    ax.set_yticklabels(["100"], minor=True)
    y_ticks_major = ax.yaxis.get_major_ticks()
    y_ticks_major[5].set_visible(False)
    ax.tick_params(axis='y', which='minor', length=0)
    plt.show()
if __name__ == '__main__':
    main()

Thanks for the help 谢谢您的帮助

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

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