类型错误:打印包含元组的数组

2024-04-19 01:46:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我只想用matplotlib解出一个非线性方程,但有一个错误是:

TypeError: zip argument #1 must support iteration

你能帮我修一下吗?。。。你知道吗

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve

r = np.arange(-100, 100, step=0.01, dtype=float)

def equation(p,r0):
    x = p
    r = r0
    return (r * x + np.power(x,3)- np.power(x,5))

temp = []

for i in r:
    x = fsolve(equation, 0, args=(i,))
    temp.extend((i,x))

my_array = np.array(temp)

#print(my_array)
x, y = zip(*my_array)
plt.plot(x,y)

Tags: importmatplotlibmyas错误nppltzip
1条回答
网友
1楼 · 发布于 2024-04-19 01:46:06

正如@Julien所说,您必须使用append而不是extend。此外,我猜您可能看不到结果,因为您的代码段中没有plt.show()。你需要在plt.plot(x,y)之后加上它。然后,输出为:

enter image description here

你最好把你最初的猜测改成别的,因为0是所有r方程的答案。例如,2的结果如下:

enter image description here

相关问题 更多 >