Python Matplotlib绘图错误:TypeError: ufunc 'bitwise_xor' not supported - 解决方法
在使用Python Matplotlib库进行绘图时,可能会遇到以下错误:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10, 10, 100)
line1,line2=plot(x,x^2,'b-',x,x^3,'r--')#绘制line1和line2
报错TypeError: ufunc 'bitwise_xor' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
这个错误是因为使用了异或运算符(^)而不是指数运算符(**)。在numpy中,要使用指数运算符来计算数组的乘方。
要修复这个错误,您需要将代码中的^替换为**。下面是修复后的代码:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10, 10, 100)
line1, line2 = plt.plot(x, x**2, 'b-', x, x**3, 'r--')
plt.show()
这样就可以正确绘制出线条了。
原文地址: https://www.cveoy.top/t/topic/pkcR 著作权归作者所有。请勿转载和采集!