2013-10-28 5 views
2

4x3 서브 플로트 격자를 플로팅하고 그 사이에 간격을 고정시키고 싶습니다. subplots_adjust를 사용하고 있습니다. 아래를 참조하십시오. 그러나 그림은 전체 창 내에 균일하게 배치되며 고정 된 공간을 갖지 않습니다. 조언 해 주셔서 감사합니다.파이썬 서브 플로트 고정 간격

import matplotlib.pyplot as plt 
import numpy as np 

data = np.random.rand(10,10) 

fig, axes = plt.subplots(4, 3) 

axes[0, 0].imshow(data) 
axes[1, 0].imshow(data) 
axes[2, 0].imshow(data) 
axes[3, 0].imshow(data) 

axes[0, 1].imshow(data) 
axes[1, 1].imshow(data) 
axes[2, 1].imshow(data) 
axes[3, 1].imshow(data) 

axes[0, 2].imshow(data) 
axes[1, 2].imshow(data) 
axes[2, 2].imshow(data) 
axes[3, 2].imshow(data) 

plt.setp(axes, xticks=[], yticks=[]) 

plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=.05, hspace=.05) 

plt.show() 

답변

3

가 당면한 문제는 subplots_adjust의 인수는 절대 값, 상대 값, 그림 폭 및 높이, 즉 분획이다 docu 참조 아닌 점이다.

"기본 캔버스"(8x6 일 수 있음)에 4 행 3 열의 정사각형 (10x10)을 플롯합니다. 그러나 그림 크기는 너비와 높이가 같고 열의 수는 행에 정의됩니다. 그래서 당신은 행과 열을 교환하고

fig, axes = plt.subplots(3, 4) 

줄거리 전화를 변경해야하고 공간이 동일합니다. 그렇지 않은 경우 figsize=(8,6)을 추가하여 그림 크기를 설정하십시오. 물론 imshow 행의 인덱스를 조정해야합니다.
enter image description here

대안으로 figsize 인수를 교환 할 수있다.

+0

덕분에, 도움이 예를 들면 다음과 같습니다이다. 그러나 나는 그림을 그리기 위해 무화과를 띄워야한다. – user2926577

1

gridspec을 사용하여 각 서브 그림의 간격과 위치를 직접 제어 할 수 있습니다. 더 많은 정보가 here입니다.

import matplotlib.gridspec as gridspec 
import matplotlib.pyplot as plt 
import numpy as np 

data = np.random.rand(10,10) 

plt.figure(figsize = (6,6)) # set the figure size to be square 

gs = gridspec.GridSpec(4, 3) 
# set the space between subplots and the position of the subplots in the figure 
gs.update(wspace=0.1, hspace=0.4, left = 0.1, right = 0.7, bottom = 0.1, top = 0.9) 


for g in gs: 
    ax = plt.subplot(g) 
    plt.imshow(data) 

plt.show() 

gridspec example