如何设置上限为“auto”,但使用matplotlib.pyp保持固定的下限

2024-05-10 08:35:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将y轴的上限设置为“自动”,但我想保持y轴的下限始终为零。我试过“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')

Tags: to代码importautoinputplottitleas
3条回答

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

plt.xlim(xmin=0)

只需将leftright传递给set_xlim

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

对于y轴,使用bottomtop

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

如前所述,根据matplotlib文档,可以使用matplotlib.axes.Axes类的^{}方法设置给定轴ax的x限制。

例如

>>> 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.gcamatplotlib.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

相关问题 更多 >