3

我想从二维 numpy 数组中针对 python 中的一维列表绘制单行。例如,我想使用 matplotlib 行 'i' 进行绘图,如下所示

  |0 0 0 0 0|
  |1 1 1 1 1|
i |2 2 2 2 2|
  |. . . . .|
  |n n n n n|

反对

[0, 100, 200, 300, 400]

我目前拥有的是:

plt.plot(list1, 2dimArray[i])

但这不起作用。当我针对 1d 列表绘制 1d 列表时,我使用了这个功能,但我不得不去多维并选择 numpy。

有没有办法做到这一点?

4

1 回答 1

2

使用下面您评论中的数据,这对我有用:

In [1]: import numpy as np

In [2]: x = np.arange(0,1100,100)

In [3]: y = np.random.rand(6,11)

In [4]: i = 2

In [5]: plt.plot(x, y[i])
Out[5]: [<matplotlib.lines.Line2D at 0x1043cc790>]

第 2 行的情节

问题是xy参数plot必须具有相同的形状(或至少相同的第一个形状条目)。

In [6]: x.shape
Out[6]: (11,)

In [7]: y.shape
Out[7]: (6, 11)

In [8]: y[i].shape
Out[8]: (11,)

也许您的程序生成的项目之一实际上并不具有您认为的形状?

如果您将列表与 numpy 数组一起使用,这也应该有效(plt.plot可能会将列表转换为数组):

In [9]: xl = range(0, 1100, 100)

In [10]: plt.plot(xl, y[i])
Out[10]: [<matplotlib.lines.Line2D at 0x10462aed0>]
于 2013-04-08T03:15:26.073 回答