2013-07-11 96 views
0

我有一個列表raws我想在ipython筆記本中繪製數組。這裏是我試圖讓工作代碼:使用matplotlib繪製數組列表

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,這樣我可以繪製它自己的每個M×N陣列其中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. ]))] 
+0

那麼問題是什麼?你遇到錯誤?它看起來很時髦嗎? – mdscruggs

+0

如果你只是想在每個子圖上有~300行,將'axes [r] .plot(raws)'改爲'axes [r] .plot(raws [r])'。這給了我兩個地塊,每個地塊都有很多線條。否則,我不清楚你想要什麼樣的情節。 – wflynny

+0

@Bill我遇到一個_italic_ValueError:用一個sequence._italic_設置一個數組元素,使用'axes [r] .plot(raws)to axes [r] .plot(raws [r])'。 – ktavabi

回答

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

+0

我可以重現您的代碼,但出於某種原因,這不適用於我的'raws'版本,它也是'list'類型的。有趣的是,當我運行'raws [0] .shape'時,我得到了AttributeError:'tuple'對象沒有屬性'shape'。我不確定什麼是'元組'?! – ktavabi

+0

元組是不變的列表對象:'(1,2,3)',而不是'[1,2,3]'。你是如何創建你的'raws'對象的?它看起來像你的numpy數組嵌套在列表中的元組。 – wflynny

+0

'raws'對象是使用讀取包含多傳感器時間序列數據的二進制文件的(很少知道的python)模塊的類函數的輸出創建的。我很抱歉,但我真的不知道還有什麼要告訴你。這裏是[函數]的鏈接(http://martinos.org/mne/generated/mne.fiff.Raw.html) – ktavabi

0

如果你h如下圖abac,你可以繪製如下圖:

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

plt.plot(*abac)