使用matplotlib绘制数组列表

时间:2013-07-11 22:01:57

标签: python matplotlib ipython-notebook

我有一个列表raws的数组,我想在ipython笔记本中绘制。以下是我正在尝试的代码:

fig, axes = subplots(len(raws),1, sharex=True, tight_layout=True, figsize=(12, 6), dpi=72)
for r in range(len(raws)):
    axes[r].plot(raws)

我已经迷失了几个小时,如果不是几天试图弄清楚如何索引列表raws,这样我就可以在它自己的轴上绘制每个mxn数组,其中n是时间点的数量,即,x轴和m是在每个点采样的时间序列函数的数量。

当我编码时:

for r in range(len(raws)):
        axes[r].plot(raws[r])

我得到一个ValueError:用序列设置数组元素。

供您参考:

    len(raws) = 2
    type(raws) = 'list'
    np.shape(raws[0][0]) = (306, 10001)
    raws = 
[(array([[ -4.13211217e-12,  -4.13287303e-12,  -4.01705259e-12, ...,
          1.36386023e-12,   1.65182851e-12,   2.00368966e-12],
       [  1.08914129e-12,   1.47828466e-12,   1.82257607e-12, ...,
         -2.70151520e-12,  -2.48631967e-12,  -2.28625548e-12],
       [ -7.80962369e-14,  -1.27119591e-13,  -1.73610315e-13, ...,
         -1.13219629e-13,  -1.15031720e-13,  -1.12106621e-13],
       ..., 
       [  2.52774254e-12,   2.32293195e-12,   2.02644002e-12, ...,
          4.20064191e-12,   3.94858906e-12,   3.69495394e-12],
       [ -4.38122146e-12,  -4.96229676e-12,  -5.47782145e-12, ...,
          3.93820033e-12,   4.18850823e-12,   4.34950629e-12],
       [ -1.07284424e-13,  -9.23447993e-14,  -7.89852400e-14, ...,
          7.92079631e-14,   5.60172215e-14,   3.04448868e-14]]), array([ 60.   ,  60.001,  60.002, ...,  69.998,  69.999,  70.   ])), (array([[ -6.71363108e-12,  -5.80501003e-12,  -4.95944514e-12, ...,
         -3.25087343e-12,  -2.68982494e-12,  -2.13637448e-12],
       [ -5.04818633e-12,  -4.65757005e-12,  -4.16084140e-12, ...,
         -4.26120531e-13,   2.20744290e-13,   7.81245614e-13],
       [  1.97329506e-13,   1.64543867e-13,   1.32679812e-13, ...,
          2.11645494e-13,   1.94795729e-13,   1.75781773e-13],
       ..., 
       [  3.04245661e-12,   2.28376461e-12,   1.54118900e-12, ...,
         -1.14020908e-14,  -8.04647589e-13,  -1.52676489e-12],
       [ -1.83485962e-13,  -5.22949893e-13,  -8.60038852e-13, ...,
          7.70312553e-12,   7.20825156e-12,   6.58362857e-12],
       [ -7.26357906e-14,  -7.11700989e-14,  -6.88759767e-14, ...,
         -1.04171843e-13,  -1.03084861e-13,  -9.68462427e-14]]), array([ 60.   ,  60.001,  60.002, ...,  69.998,  69.999,  70.   ]))]

2 个答案:

答案 0 :(得分:3)

我可以发布代码,我在这里回复。

您的数据看起来像是以

的形式嵌套
[ ( array1, array2, ..., arrayN ) ]

这可以通过以下两种方式之一来处理:

In [2]: raws = [np.random.rand(20, 100), np.random.rand(20, 100)]

In [3]: raws = raws[0]

In [4]: f, axes = plt.subplots(len(raws), 1)

In [5]: for i in range(len(raws)):
   ...:     axes[i].plot(raws[i])

或者

In [3]: raws = [(np.random.rand(20, 100), np.random.rand(20, 100))]

In [4]: f, axes = plt.subplots(len(raws[0]), 1)

In [5]: for i in range(len(raws[0])):
   ...:     axes[i].plot(raws[0][i])

enter image description here

答案 1 :(得分:0)

如果您有一个列表,例如下面的abac,您可以绘制如下:

import numpy as np
a = np.array(range(20))
b = a * 2
c = a * 3
abac = a,b,a,c

plt.plot(*abac)