図形に2つのサブプロットを追加する必要があります。1つのサブプロットは2つ目のサブプロットの約3倍の幅が必要です(高さは同じ)。私は GridSpec
と colspan
引数を使ってこれを達成しましたが、PDFに保存できるように figure
を使ってこれを行いたいと思っています。コンストラクタの figsize
引数を使って最初の図を調整することはできますが、2番目のプロットのサイズを変更するにはどうすればよいでしょうか?
gridspec](http://matplotlib.org/users/gridspec.html)と
figure`が使えます。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# plot it
fig = plt.figure(figsize=(8, 6))
gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])
ax0 = plt.subplot(gs[0])
ax0.plot(x, y)
ax1 = plt.subplot(gs[1])
ax1.plot(y, x)
plt.tight_layout()
plt.savefig('grid_figure.pdf')
.
最も簡単な方法は、Customizing Location of Subplot Using GridSpecで説明したsubplot2grid
を使うことです。
ax = plt.subplot2grid((2, 2), (0, 0))
は次のようになります。
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2)
ax = plt.subplot(gs[0, 0])
となるので、bmu'の例は
import numpy as np
import matplotlib.pyplot as plt
# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# plot it
fig = plt.figure(figsize=(8, 6))
ax0 = plt.subplot2grid((1, 3), (0, 0), colspan=2)
ax0.plot(x, y)
ax1 = plt.subplot2grid((1, 3), (0, 2))
ax1.plot(y, x)
plt.tight_layout()
plt.savefig('grid_figure.pdf')
私はpyplot
'のaxes
オブジェクトを使って、GridSpec
を使わずに手動でサイズを調整しました。
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# definitions for the axes
left, width = 0.07, 0.65
bottom, height = 0.1, .8
bottom_h = left_h = left+width+0.02
rect_cones = [left, bottom, width, height]
rect_box = [left_h, bottom, 0.17, height]
fig = plt.figure()
cones = plt.axes(rect_cones)
box = plt.axes(rect_box)
cones.plot(x, y)
box.plot(y, x)
plt.show()