plotting a list of arrays with matplotlib

15,842

Just so I can post code, I am responding here.

Looks like your data is nested in the form

[ ( array1, array2, ..., arrayN ) ]

This could be handled in one of two ways:

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

Or

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

Share:
15,842
Kambiz
Author by

Kambiz

Alas, after a certain age every man is responsible for his face. ~Camus

Updated on June 08, 2022

Comments

  • Kambiz
    Kambiz almost 2 years

    I have a list raws of arrays that I would like to plot in ipython notebook. Here is the code I am trying to get working:

    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)
    

    I've been lost for hours if not days trying to figure out how to index the list raws, such that I can plot each mxn array on it's own axis where n is the number of time points, i.e., x-axis and m is the number of time-series functions sampled at each point.

    When I code:

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

    I get an ValueError: setting an array element with a sequence.

    For your information:

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