programing

matplotlib에서 y축 한계 설정

sourcejob 2022. 9. 24. 10:22
반응형

matplotlib에서 y축 한계 설정

matplotlib의 y축 한계를 설정하는 데 도움이 필요합니다.이것이 제가 시도했지만 실패한 코드입니다.

import matplotlib.pyplot as plt

plt.figure(1, figsize = (8.5,11))
plt.suptitle('plot title')
ax = []
aPlot = plt.subplot(321, axisbg = 'w', title = "Year 1")
ax.append(aPlot)
plt.plot(paramValues,plotDataPrice[0], color = '#340B8C', 
     marker = 'o', ms = 5, mfc = '#EB1717')
plt.xticks(paramValues)
plt.ylabel('Average Price')
plt.xlabel('Mark-up')
plt.grid(True)
plt.ylim((25,250))

이 그림에 대한 데이터로 Y축 한계 20과 200을 얻습니다.하지만 20과 250을 원합니다.

다음을 통해 현재 축 가져오기plt.gca()다음으로 제한을 설정합니다.

ax = plt.gca()
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])

한 가지 방법은 matplotlib를 사용하여 축 범위를 직접 설정하는 것입니다.pyplot.axis 를 선택합니다.

matplotlib.pyplot.축

from matplotlib import pyplot as plt
plt.axis([0, 10, 0, 20])

0,10은 x축 범위, 0,20은 y축 범위입니다.

또는 matplotlib를 사용할 수도 있습니다.pyplot.xlim 또는 matplotlib.pyplot.ylim

matplotlib.pyplot.ylim

plt.ylim(-2, 2)
plt.xlim(0,10)

또 다른 해결 방법은 그림의 축을 가져와 변경되는 y 값만 다시 할당하는 것입니다.

x1,x2,y1,y2 = plt.axis()  
plt.axis((x1,x2,25,250))

다음 위치에서 개체를 인스턴스화할 수 있습니다.matplotlib.pyplot.axes에 전화합니다.set_ylim()이런 느낌일 거예요

import matplotlib.pyplot as plt
axes = plt.axes()
axes.set_ylim([0, 1])

미세 튜닝용입니다.축의 경계 중 하나만 설정하고 다른 경계는 변경하지 않으려면 다음 문 중 하나 이상을 선택할 수 있습니다.

plt.xlim(right=xmax) #xmax is your value
plt.xlim(left=xmin) #xmin is your value
plt.ylim(top=ymax) #ymax is your value
plt.ylim(bottom=ymin) #ymin is your value

xlimylim의 매뉴얼을 참조하십시오.

이것은 적어도 matplotlib 버전 2.2.2에서는 동작했습니다.

plt.axis([None, None, 0, 100])

xmin이나 ymax 전용으로 설정하는 것이 좋을지도 모릅니다.

@Hima의 답변에 현재 x 또는 y 제한을 수정하려면 다음을 사용하십시오.

import numpy as np # you probably alredy do this so no extra overhead
fig, axes = plt.subplot()
axes.plot(data[:,0], data[:,1])
xlim = axes.get_xlim()
# example of how to zoomout by a factor of 0.1
factor = 0.1 
new_xlim = (xlim[0] + xlim[1])/2 + np.array((-0.5, 0.5)) * (xlim[1] - xlim[0]) * (1 + factor) 
axes.set_xlim(new_xlim)

이 기능은 기본 플롯 설정에서 축소하거나 약간만 확대하려는 경우에 특히 유용합니다.

이거면 될 거야.타마스와 마노즈 고빈단처럼 네 코드가 나한테 효과가 있어Matplotlib 업데이트를 시도할 수 있을 것 같습니다.Matplotlib를 갱신할 수 없는 경우(예를 들어 관리자 권한이 부족한 경우)에는 다른 백엔드를 사용하여matplotlib.use()도움이 될 거야

언급URL : https://stackoverflow.com/questions/3777861/setting-y-axis-limit-in-matplotlib

반응형