0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Matplotlib覚え書き by 具体例

Posted at

画像をタイル形式でプロット

cifar-10、ランダムに選んだ100枚、タイトル付き

コード
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()

print('# Image shapes')
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

CLASSES = np.array([
    'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog',
    'frog', 'horse', 'ship', 'truck'
])

# 100個のアイテムをランダムに選びだす
idxs = np.random.randint(low=0, high=x_train.shape[0], size=100)
imgs, labels = x_train[idxs], y_train[idxs]

# ラベル(0~9整数)をクラス名に変更
labels = [CLASSES[x] for x in labels.flatten()]

# 10x10のタイルにする
fig, axs = plt.subplots(nrows=10, ncols=10, figsize=(12, 12))
# .flatで一次元アクセスできるイテレータができる
for ax, img, label in zip(axs.flat, imgs, labels):
    ax.imshow(img)
    ax.set_title(label)
    ax.set_axis_off()

# tight_layoutしないとタイトルと画像が被る
plt.tight_layout()
plt.savefig('image.png')
plt.show()
出力
# Image shapes
(50000, 32, 32, 3) (50000, 1)
(10000, 32, 32, 3) (10000, 1)

cifar-10.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?