gpt4 book ai didi

python - 使用 matplotlib 时设置 xlim 和 ylim(有些奇怪)

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:42 25 4
gpt4 key购买 nike

# 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()

最佳答案

您正在设置 set_xlimset_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_xlimset_ylim 无法调用,因为它们不存在于 pylab命名空间。 pylab.xlim 是获取当前轴对象并调用该对象的 set_xlim 方法的快捷方式。你可以自己做:

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

关于python - 使用 matplotlib 时设置 xlim 和 ylim(有些奇怪),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18808218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com