我有一个由 8 列组成的简单文本文件,我使用 loadtxt 函数读取它。我想绘制为x轴column6-column7和y轴column7-column8,所以我输入了这个命令
>>> pl.plot(np.subtract(data2[:,6], data2[:7]), np.subtract(data2[:,7], data2[:,8]))
and it gave this error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (59427) (7,9)
有什么问题吗?以及如何做到这一点?
最佳答案
data2[:7]
应该是 data2[:,7]
- 你错过了一个逗号。
data2[:7]
显然具有形状 (7,9),而 data2[:,6]
具有形状 (50427,)。错误消息表明两个数组无法广播到可以应用 np.subtract 的公共(public)形状。
x = data2[:,6] - data2[:,7]
y = data2[:,7] - data2[:,8]
pl.plot(x, y)
关于python - 在 numpy 中减去数组并用 pylab 绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19605652/