tropicbirdのブログ

備忘録です。

plt.subplots()のaxesに関するメモ。

はじめに

plt.subplots()のaxesを理解するのに時間がかかったので、記録に残す。特に、(1,5)や(5,1)など、一つの列や行のみに図をプロットするには注意が必要である。

一般的な複数表示の記述
fig,axes=plt.subplots(3,3,figsize=(5,5))
x=[i for i in np.random.randn(10000)]
y=[i for i in np.random.randn(10000)]
axes[0,0].hist(x)
axes[1,2].hist(x)

f:id:tropicbird:20191201004717p:plain

図を1つ表示

・注意点:plt.subplots(1, 1, figsize=(5,5))ではない。

fig,ax=plt.subplots(1,figsize=(5,5))
x=[i for i in np.random.randn(10000)]
ax.hist(x)

f:id:tropicbird:20191201004731p:plain

図を縦に一列に並べたい場合

・注意点:plt.subplots(2, 1, figsize=(5,5))ではない。

fig,axes=plt.subplots(2,figsize=(5,5))
x=[i for i in np.random.randn(10000)]
y=[i for i in np.random.randn(10000)]
axes[0].hist(x)
axes[1].hist(y)

f:id:tropicbird:20191201004744p:plain

図を横に一列に並べたい場合

・注意点:この場合、(ax1, ax2)のようにaxesはタプル形式にする。

fig,(ax1,ax2)=plt.subplots(1,2,figsize=(5,5))
x=[i for i in np.random.randn(10000)]
y=[i for i in np.random.randn(10000)]
ax1.hist(x)
ax2.hist(y)

f:id:tropicbird:20191201004801p:plain