Programming

matplotlib에서 y 축 제한 설정

procodes 2020. 3. 1. 16:04
반응형

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이 필요합니다.


이 시도 . 서브 플로트에서도 작동합니다.

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

귀하의 코드는 저에게도 효과적입니다. 그러나 다른 해결 방법은 플롯의 축을 얻은 다음 y 값만 변경하는 것입니다.

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


matplotlib.pyplot.axis를 사용하여 축 범위를 직접 설정하는 것이 가능합니다.

matplotlib.pyplot.axis

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)

@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.pyplot.axes하고 호출 할 수 있습니다 set_ylim(). 다음과 같습니다.

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

이 작동합니다. Tamás 및 Manoj Govindan과 같은 코드가 나를 위해 작동합니다. Matplotlib를 업데이트하려고 시도한 것 같습니다. Matplotlib를 업데이트 할 수없는 경우 (예 : 관리 권한이 충분하지 않은 경우) 다른 백엔드를 사용하면 matplotlib.use()도움 될 수 있습니다.


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

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

아마도 이것은 xmin 및 ymax 등을 설정하는 좋은 방법 일 것입니다.


문제가있는 코드 아래 코드로 생성 된 축이 첫 번째 축과 범위를 공유 하는 경우 해당 축의 마지막 플롯 이후 범위를 설정해야합니다 .

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



반응형