编程语言
首页 > 编程语言> > Python:np.vectorize返回“float”

Python:np.vectorize返回“float”

作者:互联网

运行以下代码:

import matplotlib.pyplot as plt
import numpy as np

def xon (ton, t):
    if ton <= t:
        return (t-ton)/5
    else:
        return 0

vxon = np.vectorize(xon)
t = np.linspace(0, 49, 50)    
xontest = vxon(0, t)
plt.plot(t, xontest, '-')
plt.show()

我得到的情节:
enter image description here

但是当我尝试绘制ton的值时,wchich与零不同,例如:

xontest = vxon(2, t)

该图似乎将所有xon值舍入为整数:

enter image description here

我的代码中有什么导致这种行为?

解决方法:

发现了问题.这条线

vxon = np.vectorize(xon)

应该写成

vxon = np.vectorize(xon, otypes=[np.float])

谢谢你,das-g,告诉我在哪个方向挖掘.

标签:floating,python,numpy,vectorization,piecewise
来源: https://codeday.me/bug/20190829/1759612.html