카테고리 보관물: Python

Python

Pyplot으로 모든 서브 플로트 위에 하나의 메인 타이틀을 설정하는 방법은 무엇입니까? 있습니다. 모든 서브 플롯 위에

사용하고 pyplot있습니다. 서브 플로트가 4 개 있습니다. 모든 서브 플롯 위에 하나의 메인 타이틀을 설정하는 방법은 무엇입니까? title()마지막 서브 플롯 위에 설정합니다.



답변

사용 pyplot.suptitle또는 Figure.suptitle:

import matplotlib.pyplot as plt
import numpy as np

fig=plt.figure()
data=np.arange(900).reshape((30,30))
for i in range(1,5):
    ax=fig.add_subplot(2,2,i)        
    ax.imshow(data)

fig.suptitle('Main title') # or plt.suptitle('Main title')
plt.show()

여기에 이미지 설명을 입력하십시오


답변

이것을 내 자신의 음모에 적용 할 때 유용한 몇 가지 점이 있습니다.

  • 나는 fig.suptitle(title)오히려 사용의 일관성을 선호plt.suptitle(title)
  • fig.tight_layout()제목을 사용할 때는fig.subplots_adjust(top=0.88)
  • 글꼴 크기에 대한 아래 답변을 참조하십시오

matplotlib 문서의 서브 플롯 데모 에서 가져온 예제 코드 는 마스터 제목으로 조정됩니다.

멋진 4x4 플롯

import matplotlib.pyplot as plt
import numpy as np

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

fig, axarr = plt.subplots(2, 2)
fig.suptitle("This Main Title is Nicely Formatted", fontsize=16)

axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Axis [0,0] Subtitle')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Axis [0,1] Subtitle')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Axis [1,0] Subtitle')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Axis [1,1] Subtitle')

# # Fine-tune figure; hide x ticks for top plots and y ticks for right plots
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)

# Tight layout often produces nice results
# but requires the title to be spaced accordingly
fig.tight_layout()
fig.subplots_adjust(top=0.88)

plt.show()


답변

하위 그림에도 제목이있는 경우 기본 제목 크기를 조정해야 할 수 있습니다.

plt.suptitle("Main Title", size=16)


답변