python - 使用 matplotlib 绘制数组列表

标签 python matplotlib jupyter-notebook

我有一个 raws 数组列表,我想在 ipython notebook 中绘制这些数组。这是我试图开始工作的代码:

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.   ]))]

最佳答案

为了能贴出代码,我在这里回复。

看起来您的数据嵌套在表单中

[ ( 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

关于python - 使用 matplotlib 绘制数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17604282/

相关文章:

python - 在具有共享轴的 seaborn pairplot 中显示 y_ticklabels

python - 如何在 Jupyter Notebook 中绘制/打印出框架?

dataframe - 将 QVD 文件导入 Jupyter notebook - python2

python - 用颜色变化突出显示两个图像之间的形状差异

python - APScheduler 失火测试

matplotlib - matplotlib radviz 中的关键字参数

pdf - 在 Jupyter 笔记本中使用俄语字母制作 pdf

python - 如何同时遍历多个列表

python - Python 中的私有(private)方法

python - matplotlib 与 iPython 笔记本和 python 文件的差异