python 3.4 TypeError:一元+;的操作数类型错误:';发电机&x27;

python 3.4 TypeError:一元+;的操作数类型错误:';发电机&x27;,python,Python,这是我的代码,将范围函数生成的一些浮点值传递给绘图仪: import numpy as np import matplotlib.pyplot as plt gK_inf = 20.70 gK_0 = 0.01 tauN = 0.915 + 0.037 def graph(formula, t_range): t = np.array(t_range) gK = formula(t) # <- note now we're calling the functi

这是我的代码,将范围函数生成的一些浮点值传递给绘图仪:

import numpy as np
import matplotlib.pyplot as plt

gK_inf = 20.70
gK_0 = 0.01
tauN = 0.915 + 0.037

def graph(formula, t_range):  
    t = np.array(t_range)  
    gK = formula(t)  # <- note now we're calling the function 'formula' with x
    plt.plot(t, gK)
    plt.xlabel(r"$g_K$")
    plt.ylabel(r"$\frac{P_{i}}{P_{o}}$")
    plt.legend([r"$\frac{P_{i}}{P_{o}}$"])
    annotation_string = r"$E=0$" 
    plt.text(0.97,0.8, annotation_string, bbox=dict(facecolor='red', alpha=0.5), 
         transform=plt.gca().transAxes, va = "top", ha="right")  
    plt.show()  

def my_formula(t):
    return np.power((np.power(gK_inf,0.25))*((np.power(gK_inf,0.25)-np.power(gK_0,0.25))*np.exp(-t/tauN)),4)

def frange(x, y, jump):
    while x < y:
        yield x
        x += jump

graph(my_formula, frange(0,11e-3,1e-3))
将numpy导入为np
将matplotlib.pyplot作为plt导入
gK_inf=20.70
gK_0=0.01
tauN=0.915+0.037
def图(公式,t_范围):
t=np.数组(t_范围)
gK=公式(t)#与x
>文件“F:\Eclipse\my\u test\gK”,第26行,在my\u公式中
>返回np.power((np.power(gK_inf,0.25))*((np.power(gK_inf,0.25)-np.power(gK_0,0.25))*np.exp(-t/tauN)),4)
>TypeError:一元操作数的操作数类型错误-:“generator”

您能帮忙吗?

您的
t\u范围是发电机。您尝试使用
t=np.array(t\u范围)
将其转换为numpy数组。但它不起作用。在生成器上调用
np.array
只返回一个元素数组,其中生成器是其唯一的元素。它无法展开发电机

您可以试试
np.fromiter(t\u range,dtype=np.float)
。或者先将
t_range
转换为列表


顺便说一句,您根本不清楚为什么要编写
frange
,因为它本质上与内置
范围
步骤
参数相同。

如果您想要一个浮点范围,请使用
numpy.arange
或者我按照您说的那样做:使用
np.fromiter(t_范围)
,现在我有一个错误:
TypeError:未找到必需的参数“dtype”(位置2)
>  gK = formula(t)  # <- note now we're calling the function 'formula'
> with x
>       File "F:\Eclipse\my_test\gK", line 26, in my_formula
>         return np.power((np.power(gK_inf,0.25))*((np.power(gK_inf,0.25)-np.power(gK_0,0.25))*np.exp(-t/tauN)),4)
>     TypeError: bad operand type for unary -: 'generator'