programing

Twiny를 사용할 때 Python Matplotlib 그림 제목이 축 레이블과 겹칩니다.

bestprogram 2023. 6. 11. 11:07

Twiny를 사용할 때 Python Matplotlib 그림 제목이 축 레이블과 겹칩니다.

다음과 같은 트윈을 사용하여 동일한 그래프에 두 개의 개별 수량을 표시하려고 합니다.

fig = figure()
ax = fig.add_subplot(111)
ax.plot(T, r, 'b-', T, R, 'r-', T, r_geo, 'g-')
ax.set_yscale('log')
ax.annotate('Approx. sea level', xy=(Planet.T_day*1.3,(Planet.R)/1000), xytext=(Planet.T_day*1.3, Planet.R/1000))
ax.annotate('Geostat. orbit', xy=(Planet.T_day*1.3, r_geo[0]), xytext=(Planet.T_day*1.3, r_geo[0]))
ax.set_xlabel('Rotational period (hrs)')
ax.set_ylabel('Orbital radius (km), logarithmic')
ax.set_title('Orbital charts for ' + Planet.N, horizontalalignment='center', verticalalignment='top')


ax2 = ax.twiny()
ax2.plot(v,r,'k-')
ax2.set_xlabel('Linear speed (ms-1)')

show()

데이터는 잘 표시되지만, 그림 제목이 보조 x축의 축 레이블과 겹쳐서 거의 읽을 수 없다는 문제가 있습니다(그림 예제를 여기에 게시하고 싶었지만 아직 충분히 높은 평가를 받지 못했습니다).

제목을 수십 픽셀 위로 바로 이동하여 차트가 더 예뻐 보이도록 하는 간단한 방법이 있는지 알고 싶습니다.

최신 버전의 matplotlib의 새로운 기능인지는 잘 모르겠지만, 적어도 1.3.1의 경우에는 다음과 같습니다.

plt.title(figure_title, y=1.08)

이 기능은 다음에도 적용됩니다.plt.suptitle()하지만 (아직) 그렇지 않은plt.xlabel(),기타.

사용하지 않음plt.title텍스트를 직접 배치합니다.plt.text과도하게 과장된 예는 다음과 같습니다.

import pylab as plt

fig = plt.figure(figsize=(5,10))

figure_title = "Normal title"
ax1  = plt.subplot(1,2,1)

plt.title(figure_title, fontsize = 20)
plt.plot([1,2,3],[1,4,9])

figure_title = "Raised title"
ax2  = plt.subplot(1,2,2)

plt.text(0.5, 1.08, figure_title,
         horizontalalignment='center',
         fontsize=20,
         transform = ax2.transAxes)
plt.plot([1,2,3],[1,4,9])

plt.show()

여기에 이미지 설명 입력

하위 플롯 제목과 겹치는 x 레이블에 문제가 있었습니다. 이것은 저에게 효과가 있었습니다.

import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 1)
ax[0].scatter(...)
ax[1].scatter(...)
plt.tight_layout()
.
.
.
plt.show()

전에

여기에 이미지 설명 입력

끝나고

여기에 이미지 설명 입력

참조:

ax.set_title('My Title\n', fontsize="15", color="red")
plt.imshow(myfile, origin="upper")

만약 당신이'\n'제목 문자열 바로 뒤에 그림이 제목 바로 아래에 그려집니다.그것도 빠른 해결책일 수 있습니다.

이 경우 패드를 사용할 수 있습니다.

ax.set_title("whatever", pad=20)

그냥 사용하기plt.tight_layout()전에plt.show()잘 작동합니다.

임시 솔루션을 사용할 수 있습니다.x,y당신의 직함의 위치.

저는 팔로잉이 효과가 있었습니다.

plt.title('Capital Expenditure\n') # Add a next line after your title

칭찬의 말

사용plt.tight_layout()이전에plt.show()저한테는 잘 맞습니다.

패딩을 추가하여 더 낫고 더 잘 보이게 할 수도 있습니다.

ax.set_title("title", pad=15)

언급URL : https://stackoverflow.com/questions/12750355/python-matplotlib-figure-title-overlaps-axes-label-when-using-twiny