Massyanya
Massyanya

Reputation: 2934

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.

Upvotes: 0

Views: 1056

Answers (1)

tmdavison
tmdavison

Reputation: 69116

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()

enter image description here

Upvotes: 2

Related Questions