简体   繁体   English

Matplotlib:删除子图的轴上的编号,该子图是用fig.add_subplot()创建的

[英]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() )? 假设我使用fig.add_subplot()创建子图(与plt.subplots()相对fig.add_subplot() ,我有没有使用matplotlib删除子图的轴之一上的编号?

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. 我想知道如何摆脱子图第一行中x轴上的数字。

You can use ax.set_xticklabels([]) to remove the tick labels from a given subplot. 您可以使用ax.set_xticklabels([])从给定的子图中删除刻度线标签。 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: 在您的情况下,其索引为1、2和3。因此,您可以仅使用if index < 4:如下所示:

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

在此处输入图片说明

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

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