【问题标题】:How do I add titles and get rid of the axis with just plt.figure如何添加标题并仅使用 plt.figure 摆脱轴
【发布时间】:2021-10-27 20:32:27
【问题描述】:

我想为每个子图添加标题,并去掉其中的 x 轴和 y 轴。

fig = plt.figure(figsize=(6,6)) # specifying the overall grid size

for i in range(16):
    plt.subplot(4,4,i+1)    # the number of images in the grid is 6*6 (16)
    img = mpimg.imread(f'../input/cifar10-mu/train_images/{train["filename"][i]}')
    plt.imshow(img)
    
fig.suptitle('Sample Images')
plt.show()

这是上面代码的输出

【问题讨论】:

标签: python matplotlib


【解决方案1】:

你可以添加这两行:

plt.text(0, 0, f'Label {train["label"][i]}')
plt.axis('off')

让整个代码看起来像这样:

fig = plt.figure(figsize=(6,6)) # specifying the overall grid size

for i in range(16):
    plt.subplot(4,4,i+1)    # the number of images in the grid is 6*6 (16)
    img = mpimg.imread(f'../input/cifar10-mu/train_images/{train["filename"][i]}')
    plt.imshow(img)
    plt.text(0, 0, f'Label {train["label"][i]}')
    plt.axis('off')
    
fig.suptitle('Sample Images')
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2019-02-05
    相关资源
    最近更新 更多