TypeError:只有长度为1的数组可以转换为Python标量-复数

时间:2019-01-24 09:25:50

标签: python python-2.7 numpy matplotlib math

我尝试使用Python 2.7绘制具有复杂值的图形。但是代码没有返回y值的复数。

import numpy as np
import math
import cmath
import matplotlib.pyplot as plt

def f(x):
     return ((x)*cmath.sqrt((x)+0.5)*((x)+1))


x=np.linspace(-4,-2,10)
y=f(x)
plt.plot( x,y, label=fx_name)
plt.title('sample ')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend(loc='upper left')
plt.show()

错误是

    Traceback (most recent call last):
  File "/home/Happy/Demo/code10.py", line 12, in <module>
    y=f(x)
  File "/home/Happy/Demo/code10.py", line 7, in f
    return ((x)*cmath.sqrt((x)+0.5)*((x)+1))
TypeError: only length-1 arrays can be converted to Python scalars

1 个答案:

答案 0 :(得分:0)

使用numpy尝试:

<button id="refresh">Get Random Address</button>
<span id="output"></span>