简体   繁体   中英

Matplotlib: Removing the numbering on an axes of the subplot, created with fig.add_subplot()

I there away to remove the numbering on one of the axes of the subplot using matplotlib, assuming I use fig.add_subplot() for creating subplots (as opposed to plt.subplots() )?

Here is the example code:

index = 0
fig = plt.figure()

for q in range(3, 24, 4):
    index  += 1;
    f = './npy_train_{:02d}_0013.npy'.format(q)
    images = np.load(f)
    ax = fig.add_subplot(2, 3, index)
    ax.set_xlim((0,1)); ax.set_ylim((0,1))
    ax.plot(images[0, 0, :, 0], images[0, 1, :, 0], 'bo', label = 'train_{:02d}_0013.npy'.format(q))
    ax.set_title('npy_train_{:02d}_0013.npy'.format(q))

I would like to know how to get rid of the numbers on x-axis in the first row of subplots.

You can use ax.set_xticklabels([]) to remove the tick labels from a given subplot. Then you just need to know which subplots to apply this to. In your case, its those with indices 1, 2 and 3. So, you can just use if index < 4: , like so:

import matplotlib.pyplot as plt
import numpy as np

index = 0
fig = plt.figure()

for q in range(3, 24, 4):
    index  += 1;
    f = './npy_train_{:02d}_0013.npy'.format(q)
    images = np.load(f)
    ax = fig.add_subplot(2, 3, index)
    ax.set_xlim((0,1)); ax.set_ylim((0,1))
    ax.plot(images[0, 0, :, 0], images[0, 1, :, 0], 'bo', label = 'train_{:02d}_0013.npy'.format(q))
    ax.set_title('npy_train_{:02d}_0013.npy'.format(q))

    if index < 4:
        ax.set_xticklabels([])

plt.show()

在此处输入图片说明

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