如何为上限设置“自动”,但通过matplotlib.pyplot保持固定的下限

问题:如何为上限设置“自动”,但通过matplotlib.pyplot保持固定的下限

我想将y轴的上限设置为“自动”,但我想使y轴的下限始终为零。我尝试了“自动”和“自动调整范围”,但它们似乎不起作用。先感谢您。

这是我的代码:

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')

I want to set the upper limit of the y-axis to ‘auto’, but I want to keep the lower limit of the y-axis to always be zero. I tried ‘auto’ and ‘autorange’, but those don’t seem to work. Thank you in advance.

Here is my code:

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')

回答 0

您可以通过只leftrightset_xlim

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

对于y轴,请使用bottomtop

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

You can pass just left or right to set_xlim:

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

For the y axis, use bottom or top:

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

回答 1

只需设置xlim以下限制之一:

plt.xlim(xmin=0)

Just set xlim for one of the limits:

plt.xlim(xmin=0)

回答 2

如上所述,根据matplotlib文档,ax可以使用类的set_xlim方法设置给定轴的x极限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_ylimmatplotlib.pyplot.ylim。关键字参数为topbottom

As aforementioned and according to the matplotlib documentation, the x-limits of a given axis ax can be set using the set_xlim method of the matplotlib.axes.Axes class.

For instance,

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

One limit may be left unchanged (e.g. the left 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)

To set the x-limits of the current axis, the matplotlib.pyplot module contains the xlim function that just wraps matplotlib.pyplot.gca and 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

Similarly, for the y-limits, use matplotlib.axes.Axes.set_ylim or matplotlib.pyplot.ylim. The keyword arguments are top and bottom.


回答 3

只需在@silvio的点上添加一个点:如果使用轴绘制像figure, ax1 = plt.subplots(1,2,1)。然后ax1.set_xlim(xmin = 0)也可以!

Just add a point on @silvio ‘s: if you use axis to plot like figure, ax1 = plt.subplots(1,2,1). Then ax1.set_xlim(xmin = 0) also works!