2012-07-31 64 views
64

y軸の上限を 'auto'に設定したいが、下限を維持したいy軸は常にゼロになります。私は「オート」と「オートレンジ」を試みましたが、それらは機能していないようです。前もって感謝します。上限を 'auto'に設定する方法はmatplotlib.pyplotで固定下限を維持する方法

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

答えて

66

あなたはset_xlimにちょうどleftまたはrightを渡すことができます:ここで

は私のコードで、y軸については

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

使用bottomまたはtop

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

set_ylimの左側を通過したときにエラーが発生しました。 私はこれを代わりに使用しました:plt.gca()。set_ylim(ymin = 0) ありがとうございました。 – vietnastee

+0

'plt.xlim'または' plt.ylim'を使って、現在の軸の制限を設定することもできます。 – Chris

+18

これを行うと、ウィンドウがインスタンス化するどんな値にも上限が付きます。自動スケーリングのままではありません。 – Elliot

2

だけ@silvioの上の点を追加します。あなたはfigure, ax1 = plt.subplots(1,2,1)のようにプロットするために軸を使用している場合。その後、ax1.set_xlim(xmin = 0)も機能します!

3

前述したように、matplotlibのドキュメントによれば、特定の軸のx-limitは、クラスのset_xlimメソッドを使用して設定することができます。例えば

>>> 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モジュールが含まれていますとmatplotlib.axes.Axes.set_xlimをラップする機能が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を使用します。キーワード引数はtopbottomです。

関連する問題