3
# the first plot DOES NOT set the xlim and ylim properly 
import numpy as np
import pylab as p

x = np.linspace(0.0,5.0,20)
slope = 1.0 
intercept = 3.0 
y = slope*x + intercept
p.set_xlim = ([0.0,10.0])
p.set_ylim = ([0.0,10.0])
p.plot(x,y)
p.show()
p.clf()

def xyplot():
    slope = 1.0
    intercept = 3.0
    x = np.linspace(0.0,5.0,20)
    y = slope*x + intercept 
    p.xlim([0.0,10.0])
    p.ylim([0.0,10.0])
    p.plot(x,y)
    p.show()

# if I place the same exact code a a function, the xlim and ylim
# do what I want ...

xyplot()    
4

1 に答える 1

7

あなたはそれを呼び出す代わりに設定set_xlimしています。set_ylimあなたが持っている場所:

p.set_xlim = ([0.0,10.0])
p.set_ylim = ([0.0,10.0])

あなたが持っている必要があります:

p.set_xlim([0.0,10.0])
p.set_ylim([0.0,10.0])

その変更を行うと、名前空間に存在しないためset_xlimandset_ylimを呼び出すことができないことに気付くでしょう。現在の軸オブジェクトを取得し、そのオブジェクトのメソッドを呼び出すショートカットです。あなたはこれを自分で行うことができます:pylabpylab.xlimset_xlim

ax = p.subplot(111)
ax.set_xlim([0.0,10.0])
ax.set_ylim([0.0,10.0])
于 2013-09-15T12:53:31.313 に答える