sourcetip

상한은 '자동'으로 설정하되 하한은 고정하는 방법

fileupload 2023. 7. 17. 21:21
반응형

상한은 '자동'으로 설정하되 하한은 고정하는 방법

저는 y축의 상한을 'auto'로 설정하고 싶지만, y축의 하한을 항상 0으로 유지하고 싶습니다.'auto'와 'autorange'를 시도해 보았지만 작동하지 않는 것 같습니다.

내 코드는 다음과 같습니다.

import matplotlib.pyplot as plt

def plot(results_plt,title,filename):

    ############################
    # Plot results

    # mirror result table such that each parameter forms an own data array
    plt.cla()
    #print results_plt
    XY_results = []

    XY_results = zip( *results_plt)

    plt.plot(XY_results[0], XY_results[2], marker = ".")

    plt.title('%s' % (title) )
    plt.xlabel('Input Voltage [V]')
    plt.ylabel('Input Current [mA]')

    plt.grid(True)
    plt.xlim(3.0, 4.2)  #***I want to keep these values fixed"
    plt.ylim([0, 80]) #****CHANGE**** I want to change '80' to auto, but still keep 0 as the lower limit 
    plt.savefig(path+filename+'.png')

그냥 지나쳐도 됩니다.left또는right로.set_xlim:

plt.gca().set_xlim(left=0)

y 축에 대해 다음을 사용합니다.bottom또는top:

plt.gca().set_ylim(bottom=0)

중요한 참고: "데이터를 플롯한 후에는 함수를 사용해야 합니다.이 작업을 수행하지 않으면 왼쪽/아래쪽에는 기본값 0이 사용되고 위쪽/오른쪽에는 기본값 1이 사용됩니다." - Luc의 대답.

설정하기xlim제한 중 하나에 대해:

plt.xlim(left=0)

앞서 언급한 바와 같이 matplotlib 설명서에 따르면, 주어진 축의 x-한계ax의 방법을 사용하여 설정할 수 있습니다.matplotlib.axes.Axes학급.

예를 들어.

>>> ax.set_xlim(left_limit, right_limit)
>>> ax.set_xlim((left_limit, right_limit))
>>> ax.set_xlim(left=left_limit, right=right_limit)

하나의 한계를 변경하지 않고 유지할 수 있습니다(예: 왼쪽 한계).

>>> ax.set_xlim((None, right_limit))
>>> ax.set_xlim(None, right_limit)
>>> ax.set_xlim(left=None, right=right_limit)
>>> ax.set_xlim(right=right_limit)

현재 축의 x 한계를 설정하려면,matplotlib.pyplot모듈은 다음을 포함합니다.xlim그냥 마무리하는 함수matplotlib.pyplot.gca그리고.matplotlib.axes.Axes.set_xlim.

def xlim(*args, **kwargs):
    ax = gca()
    if not args and not kwargs:
        return ax.get_xlim()
    ret = ax.set_xlim(*args, **kwargs)
    return ret

마찬가지로 y-한계의 경우 다음을 사용합니다.matplotlib.axes.Axes.set_ylim또는matplotlib.pyplot.ylim키워드 인수는 다음과 같습니다.top그리고.bottom.

set_xlim그리고.set_ylim허용하다None이를 달성하기 위한 가치.그러나 데이터를 표시한 후에는 함수를 사용해야 합니다.이렇게 하지 않으면 왼쪽/아래쪽에 기본값 0이 사용되고 위쪽/오른쪽에 기본값 1이 사용됩니다.한계치를 설정한 후에는 새 데이터를 표시할 때마다 "자동" 한계치가 다시 계산되지 않습니다.

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([0, 1, 4, 5], [3, 5, 6, 9])
ax.set_xlim(left=2, right=None)
ax.set_ylim(bottom=None, top=7)

plt.show()

(즉, 위의 예에서, 당신이 원한다면,ax.plot(...)마지막에, 그것은 원하는 효과를 주지 않을 것입니다.)

@silvio의 점을 추가하면 됩니다: 축을 사용하여 다음과 같은 그림을 그릴 경우.figure, ax1 = plt.subplots(1,2,1).그리고나서ax1.set_xlim(xmin = 0)효과도 있습니다!

다음 작업도 수행할 수 있습니다.

ax.set_xlim((None,upper_limit))
ax.set_xlim((lower_limit,None))

여러 파라미터를 동시에 설정할 수 있는 set()를 사용하려는 경우 유용합니다.

ax.set(xlim=(None, 3e9), title='my_title', xlabel='my_x_label', ylabel='my_ylabel')

언급URL : https://stackoverflow.com/questions/11744990/how-to-set-auto-for-upper-limit-but-keep-a-fixed-lower-limit

반응형