2014-01-23 38 views
0

比方說,我們有一個列表,其中包含另一個列表與一些數據,在我的情況下,頻率。如何繪製穩定圖?

model_order = [[], 
       [1], 
       [1.1], 
       [1, 1.5], 
       [1, 1.55, 3.5], 
       [1.1, 1.45, 3.45, 3.5, 4.8]] 

正如您所看到的,嵌套列表具有不同的長度,也可以爲空。

我要繪製像on the picture圖,

enter image description here

而是停留在如何使用python和matplotlib做到這一點。我該怎麼做? (1,len(model_order [i]),len(model_order [ i]))作爲x軸的數據。但是對於具有相同第一維度的x和y數據的必要條件。

from matplotlib import pyplot 
import numpy as np 

def plot_stabilization_diagram(data): 

    pyplot.figure(figsize=(8, 6), dpi=80) 
    pyplot.subplot(1, 1, 1) 

    yaxis = np.linspace(1, len(data), len(data)) 

    for d in xrange(len(data)): 
     pyplot.plot(data[d], yaxis, 'o') 

    pyplot.show() 


>>> ValueError: x and y must have same first dimension 
+0

你能告訴我們你已經嘗試了什麼?這寫作'請爲我做我的工作',這往往會惹惱人們。 – tacaswell

回答

0

您需要的順序相同的數組作爲model_order - 要做到這一點創建np.ones_like一個數組,np.zeros_like - 那麼你可以把你想要的任何值通過它遍歷這個數組。然後您需要遍歷數組輸入中的每個數組以獲取每個項目的繪圖。

from matplotlib import pyplot 
import numpy as np 

def plot_stabilization_diagram(data): 

    pyplot.figure(figsize=(8, 6), dpi=80) 
    pyplot.subplot(1, 1, 1) 

    yaxis = np.ones_like(data) 

    for pos, row in enumerate(data): 
     for pos2, item in enumerate(row): 
      pyplot.plot(item, yaxis[pos2], 'o') 

    pyplot.show() 

def plot_stabilization_diagram_2(data): 

    pyplot.figure(figsize=(8, 6), dpi=80) 
    pyplot.subplot(1, 1, 1) 

    for pos, row in enumerate(data): 
     for pos2, item in enumerate(row): 
      pyplot.plot(pos, item, 'o') 

    pyplot.show() 

model_order = [[], 
      [1], 
      [1.1], 
      [1, 1.5], 
      [1, 1.55, 3.5], 
      [1.1, 1.45, 3.45, 3.5, 4.8]] 

plot_stabilization_diagram(model_order) 

您的代碼還將model_order的所有頻率放到x軸上,而不是y軸上。

第二個版本根據行號繪製頻率 - 更像您的示例圖像中的內容。

http://imgur.com/a/CQgcU

+1

謝謝,rabs!您的解決方案有效 –