matplotlibのメモ

覚えておく点

  • matplotlibの使い方をすぐ忘れる(特に、subplot)。この書き方にこだわる必要は無いけれど、要点だけまとめておく。
  • 手順としては、figureを作る(大きなキャンバスを定義)、figureに対してadd_subplotでサブキャンバスを作る。これはaxesって言うオブジェクトらしい。で、axesに対して描画コマンドを送る。複数のサブキャンバスを入れる場合はこれを繰り返す。

imshow

  • MNISTとかを表示するときに,2Dデータはmatplotlibのimshowで簡単に表示可能。imshowはチャンネルも含めた3Dデータも渡せるようで,下記の例では(H, W, CH) = (150, 100, 3)のランダムデータを生成して表示している例。
  • axesにまたがったタイトルはfig.suptitleにて設定する。
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10, 10)) # unit is inch
fig.suptitle('Super title')
# Add a subplot and draw on it.
ax1 = fig.add_subplot(2, 1, 1) # row, col, idx
ax1.set_title('Graph title is here.')
ax1.set_xlabel('x-label')
ax1.set_ylabel('y-label')
x = np.linspace(0, 2*3.14, 100)
y = np.sin(x)
ax1.plot(x, y, '-')

# Add another subplot and draw an image on it.
ax2 = fig.add_subplot(2, 1, 2) # row, col, idx
rand_img = np.random.randn(150, 100, 3)
ax2.imshow(rand_img, cmap='gray')

plt.show()