Matplotlib 다른 크기 하위 플로트 그림에 두 개의 하위

그림에 두 개의 하위 그림을 추가해야합니다. 하나의 서브 플롯은 두 번째 (같은 높이)보다 약 3 배 넓어야합니다. 나는 이것을 사용 GridSpec하여 colspan인수를 달성 했지만 figurePDF로 저장할 수 있도록 이것을 사용 하고 싶습니다. figsize생성자 의 인수를 사용하여 첫 번째 그림을 조정할 수 있지만 두 번째 플롯의 크기를 어떻게 변경합니까?



답변

다른 방법은 subplots함수 를 사용 하고 너비 비율을 gridspec_kw다음 과 같이 전달하는 것입니다 .

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
f, (a0, a1) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1]})
a0.plot(x, y)
a1.plot(y, x)

f.tight_layout()
f.savefig('grid_figure.pdf')

답변

당신은 사용할 수 있습니다 gridspecfigure:

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')


답변

아마도 가장 간단한 방법은 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')

답변

내가 사용 pyplotaxes직접 사용하지 않고 크기를 조정하는 객체를 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()