使用matplotlib绘制2d数组

问题描述 投票:1回答:1
a = np.arange(1,10).reshape((3,3))

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

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

python numpy matplotlib numpy-ndarray
1个回答
0
投票

这取决于你想要达到的目标。显然,您的尺寸不适合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()
© www.soinside.com 2019 - 2024. All rights reserved.