首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将上限设置为'auto‘,但使用matplotlib.pyplot保持固定的下限

如何将上限设置为'auto‘,但使用matplotlib.pyplot保持固定的下限
EN

Stack Overflow用户
提问于 2012-08-01 00:41:07
回答 5查看 83.6K关注 0票数 139

我希望将y轴的上限设置为'auto',但我希望y轴的下限始终为零。我试过'auto‘和'autorange',但它们似乎不起作用。提前谢谢你。

下面是我的代码:

代码语言:javascript
复制
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')
EN

回答 5

Stack Overflow用户

发布于 2012-08-01 00:58:24

您可以只将leftright传递给set_xlim

代码语言:javascript
复制
plt.gca().set_xlim(left=0)

对于y轴,请使用bottomtop

代码语言:javascript
复制
plt.gca().set_ylim(bottom=0)
票数 133
EN

Stack Overflow用户

发布于 2015-09-17 23:16:04

只需为其中一个限制设置xlim

代码语言:javascript
复制
plt.xlim(left=0)
票数 45
EN

Stack Overflow用户

发布于 2017-09-22 18:08:08

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

例如,

代码语言:javascript
复制
>>> ax.set_xlim(left_limit, right_limit)
>>> ax.set_xlim((left_limit, right_limit))
>>> ax.set_xlim(left=left_limit, right=right_limit)

一个限制可以保持不变(例如,左侧限制):

代码语言:javascript
复制
>>> 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模块包含包装matplotlib.pyplot.gcamatplotlib.axes.Axes.set_xlimxlim函数。

代码语言:javascript
复制
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

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11744990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档