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

标签 python matplotlib

# 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/

相关文章:

python - 解析 JSON 以查找键的值

python - 如何正确排序 Python 字典中的项目?

python - Tornado .web : Is there a method which is called after the actual handler method?

python - 删除 seaborn 线图图例标题

python - 如何调整matplotlib中每第二行子图之间的空间

python - 为有子图的图形添加标题

python - 来自位于边界上的一组点的多边形

python - 通过游标从 mySQL 中选择记录返回一个 None 对象

python - 试图按百分比拆分列表

python - 在pylab中显示X轴和y轴