用 matplotlib 绘制 2darray

Plotting 2darray with matplotlib

a = np.arange(1,10).reshape((3,3))

plt.plot(a[0],a[1:])

为什么我会收到:ValueError:x 和 y 必须具有相同的第一维错误?

这取决于你想达到什么目的。 显然,您的尺寸不适合 x 轴和 y 轴。 您可以只转置 a[1:] 以沿 a[0] 定义的轴绘制两条线,如下所示:

import matplotlib.pyplot as plt
import numpy as np
a = np.arange(1,10).reshape((3,3))
print("Shape of a: " + str(a.shape)) # Shape of a: (3, 3)
print("Shape of a[0]: " + str(a[0].shape)) # Shape of a[0]: (3,)
print("Shape of a[1:]: " + str(a[1:].shape)) # Shape of a[1:]: (2, 3)
print("Shape of a[1:].T: " + str(a[1:].T.shape)) # Shape of a[1:].T: (3, 2)
plt.plot(a[0],a[1:].T)
plt.show()