Python Matplotlib 绘图报错: ^ 运算符无法用于 NumPy 数组
这个错误是因为在使用' ^ '运算符时,numpy数组不支持按位异或操作。要解决这个问题,可以使用numpy的power函数来计算x的平方和x的立方。
下面是修改后的代码:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10, 10, 100)
line1, = plt.plot(x, np.power(x, 2), 'b-')
line2, = plt.plot(x, np.power(x, 3), 'r--')
plt.show()
这样修改后的代码将正确绘制出x的平方和x的立方曲线,并显示在一个图形窗口中。
原文地址: https://www.cveoy.top/t/topic/pkcM 著作权归作者所有。请勿转载和采集!