6

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.   ]))]
4
  • So what's the problem? Are you encountering an error? Does it look funky? Commented Jul 11, 2013 at 22:08
  • If you just want on each subplot to have ~300 lines, change axes[r].plot(raws) to axes[r].plot(raws[r]). That gives me two plots with lots of lines on each. Otherwise, it's not clear to me what kind of plots you want. Commented Jul 11, 2013 at 22:10
  • @Bill I encounter a italic_ValueError: setting an array element with a sequence._italic with either axes[r].plot(raws) to axes[r].plot(raws[r]). Commented Jul 11, 2013 at 22:30
  • @mdscruggs all I get are empty axes. Commented Jul 11, 2013 at 22:31

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

4 Comments

I can reproduce your code, but for some reason this doesn't work with my version of 'raws' which is also of type 'list'. Interestingly though when I run raws[0].shape I get AttributeError: 'tuple' object has no attribute 'shape'. I am not exactly sure what a 'tuple' is?!
Tuples are immutable list objects: (1, 2, 3) as opposed to [1, 2, 3]. How did you create your raws object? It looks like your numpy arrays are nested in tuples in a list.
The raws object was created using output from a class function of a (little known python) module that reads binary files containing multi-sensor time series data. I'm sorry but I really don't know what else to tell you. Here is a link to the function
Hmm, seems like the data is just in the form [ ( array1, ..., arrayN ) ]. Check out the edited post and see if that helps.
-1

If you have a list of arrays such as abac below, you can plot as following:

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

plt.plot(*abac)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.