简体   繁体   中英

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.

Also, as you can see I accessed y_ticks[-1] to set its label to be invisible. 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.

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 . 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) . 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:

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:

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 .

What happens is: You're getting the list of y_ticks after you've created an axes, but before you plot anything. So matplotlib sets up an axes with six ticks on both axes, from 0.0 to 1.0, and gives you the tick list.

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.

Solution: Plot first, then 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. Therefore I accessed it by ax.tick_params(axis='y', which='minor', length=0) . This however will set ALL minor ticks according to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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