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)

답변

객체를 인스턴스화 matplotlib.pyplot.axes하고 호출 할 수 있습니다 set_ylim(). 다음과 같습니다.

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

답변

이것은 적어도 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)

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


답변

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