itsource

matplotlib에서 하위구에 xlim 및 ylim을 설정하는 방법

mycopycode 2023. 1. 19. 07:09
반응형

matplotlib에서 하위구에 xlim 및 ylim을 설정하는 방법

특정 하위구에 대해 matplotlib의 X축과 Y축을 제한하려고 합니다.하위구 그림 자체에는 축 속성이 없습니다.예를 들어 두 번째 그림에 대한 한계만 변경하려고 합니다.

import matplotlib.pyplot as plt
fig=plt.subplot(131)
plt.scatter([1,2],[3,4])
fig=plt.subplot(132)
plt.scatter([10,20],[30,40])
fig=plt.subplot(133)
plt.scatter([15,23],[35,43])
plt.show()

matplotlib에는 스테이트 머신인터페이스가 아닌 OO 인터페이스를 사용해야 합니다.거의 모든 것이plt.*기능은 기본적으로 다음과 같은 얇은 포장지입니다.gca().*.

plt.subplot 오브젝트를 반환합니다.축 객체에 대한 참조가 있으면 해당 객체에 직접 플롯하거나 한계를 변경할 수 있습니다.

import matplotlib.pyplot as plt

ax1 = plt.subplot(131)
ax1.scatter([1, 2], [3, 4])
ax1.set_xlim([0, 5])
ax1.set_ylim([0, 5])


ax2 = plt.subplot(132)
ax2.scatter([1, 2],[3, 4])
ax2.set_xlim([0, 5])
ax2.set_ylim([0, 5])

원하는 만큼 많은 축을 사용할 수 있습니다.

또는 모든 것을 루프 형태로 정리하는 것이 좋습니다.

import matplotlib.pyplot as plt

DATA_x = ([1, 2],
          [2, 3],
          [3, 4])

DATA_y = DATA_x[::-1]

XLIMS = [[0, 10]] * 3
YLIMS = [[0, 10]] * 3

for j, (x, y, xlim, ylim) in enumerate(zip(DATA_x, DATA_y, XLIMS, YLIMS)):
    ax = plt.subplot(1, 3, j + 1)
    ax.scatter(x, y)
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)

언급URL : https://stackoverflow.com/questions/15858192/how-to-set-xlim-and-ylim-for-a-subplot-in-matplotlib

반응형