matplotlib描画サブマップsubplot


1.サブ図を描く
subplot()メソッドを使用して、subplot()メソッドはnumrows、numcols、fignumパラメータを入力します.fignum範囲は1からnumrows*numcolsです.
import matplotlib.pyplot as plt
import numpy as np

x=np.linspace(0.0, 5.0, 50)
y1 = np.sin(x)
y2 = np.exp(x) * np.cos(x)

figures = plt.figure(1)
axeses = [plt.subplot(221), plt.subplot(224)]

plt.subplot(221)
lines=plt.plot(x, y1, x, y2)
plt.subplot(224)
plt.plot(x)

plt.setp(figures, facecolor='c')
plt.setp(lines, linestyle='dashdot')

plt.show()

また、描画されたサブマップは矩形メッシュ上になくaxes()コマンドを使用して、指定された位置でaxes([left,bottom,width,height])で描画することもでき、すべてのパラメータ値が小数(0 to 1)座標、すなわち元の図との比である.
import matplotlib.pyplot as plt
import numpy as np
#     

t = np.arange(0.0, 10.0, 0.01)
r = np.exp(-t[:1000]/0.05) # impulse response
x = np.random.randn(len(t))
s = np.convolve(x, r)[:len(x)]*dt # colored noise

#      axes subplot(111)
plt.plot(t, s)
plt.axis([0, 1, 1.1*np.amin(s), 2*np.amax(s)])

#   
a = plt.axes([.65, .6, .2, .2], facecolor='y')
n, bins, patches = plt.hist(s, 400, normed=1)
plt.title('Probability')
plt.xticks([])
plt.yticks([])

#       
a = plt.axes([0.2, 0.6, .2, .2], facecolor='y')
plt.plot(t[:len(r)], r)
plt.title('Impulse response')
plt.xlim(0, 0.2)
plt.xticks([])
plt.yticks([])

plt.show()

2.スケッチの詳細
MATLABとpyplotは,いずれも現在のフレームと現在のサブマップの概念を持っている.これまですべての描画コマンドが現在のサブ図に適用されていました.関数gca()get current axesは現在のサブマップ(matplotlib.axes.Axesインスタンス)を返し、gcf()get current figureは現在のフレーム(matplotlib.figure.Figureインスタンス)を返す.
3.GridSpecによるSubplotレイアウトのカスタマイズ
GridSpecは、サブマップに配置されるジオメトリメッシュを指定します.
3.1 subplotgrid例
subplot 2 gridは「pyplot.subplot」に似ていますが、0からインデックスが開始されます.次の2つの文の効果は同じです.
ax = plt.subplot2grid((2,2),(0, 0))
ax = plt.subplot(2,2,1)

subplot 2 grid複数のセルにまたがるサブマップを作成
ax1 = plt.subplot2grid((3,3), (0,0), colspan=3)
ax2 = plt.subplot2grid((3,3), (1,0), colspan=2)
ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3,3), (2, 0))
ax5 = plt.subplot2grid((3,3), (2, 1))

3.2 GridSpecとSubplotSpecを使用
GridSpecを明示的に作成してサブマップを作成できます
ax = plt.subplot2grid((2,2),(0, 0))

に相当
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2)
ax = plt.subplot(gs[0, 0])

gridspecは、類似配列のインデックスを提供してSubplotSpecインスタンスを返すので、スライス(slice)を使用してセルをマージすることもできます.
gs = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(gs[0, :])
ax2 = plt.subplot(gs[1,:-1])
ax3 = plt.subplot(gs[1:, -1])
ax4 = plt.subplot(gs[-1,0])
ax5 = plt.subplot(gs[-1,-2])

3.3 GridSpecレイアウトの調整
GridSpecインスタンスが表示されると、gridspecから作成されたサブマップのレイアウトパラメータを調整できます.
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)

これはtosubplotsに似ています.adjust(以前のサブマップリストパラメータ設定)は、指定されたGridSpecから作成されたサブマップにのみ影響します.例:
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)
ax1 = plt.subplot(gs1[:-1, :])
ax2 = plt.subplot(gs1[-1, :-1])
ax3 = plt.subplot(gs1[-1, -1])

gs2 = gridspec.GridSpec(3, 3)
gs2.update(left=0.55, right=0.98, hspace=0.05)
ax4 = plt.subplot(gs2[:, :-1])
ax5 = plt.subplot(gs2[:-1, -1])
ax6 = plt.subplot(gs2[-1, -1])

リファレンス
matplotlib中国語マニュアル