2017-08-15 65 views
0

我玩弄在Python,我想創建一個發生在一個二維的清單,由1和0的每個列表中的一步圖形功能,以及各個曲線名單上的一個單獨的行,與此類似: clocking graph enter image description here要在圖中的線之間創造空間與階躍函數MatPlotLib

當我運行我的代碼,它看起來是這樣的:Code output enter image description here

它可以是一個有點難以閱讀,尤其是在使用一個更大的時名單。我想在圖線之間創建空間以使其更具可讀性。任何幫助,將不勝感激。

# supress warning message 
import warnings; warnings.simplefilter("ignore") 
# extension libraries 
import matplotlib.pyplot as plt 
import numpy as np 


bits = [[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \ 
     [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \ 
     [0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1]] 

for i in range(len(bits)): 
    data = np.repeat(bits[i], 2) 
    t = 0.5 * np.arange(len(data)) 

    plt.hold(True) 
    plt.step(t, data + i, linewidth=1.5, where='post', color='g') 

    plt.ylim([-1, 10]) 

    # Labels the graphs with binary sequence 
    for tbit, bit in enumerate(bits[i]): 
     plt.text(tbit + 0.2, i, str(bit), fontsize=12, color='g') 

    # removes the built in graph axes and prints line every interation 
    plt.gca().axis('off') 


plt.show() 

回答

0

您只需通過增加y值來增加每條線的垂直間距。爲了方便,我簡單地乘以i 2到空間的線更遠了一點。如果你想在線條的位置更多的控制,你可以有y值的列表,並遍歷該網址。

bits = [[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \ 
     [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \ 
     [0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1]] 

for i in range(len(bits)): 
    data = np.repeat(bits[i], 2) 
    t = 0.5 * np.arange(len(data)) 

    plt.hold(True) 
    plt.step(t, data + i*2, linewidth=1.5, where='post', color='g') 
    #     ^^^^^^ 

    plt.ylim([-1, 10]) 

    # Labels the graphs with binary sequence 
    for tbit, bit in enumerate(bits[i]): 
     plt.text(tbit + 0.2, 0.25+i*2, str(bit), fontsize=12, color='g') 
    #      ^^^^^^^^^ 

    # removes the built in graph axes and prints line every interation 
    plt.gca().axis('off') 

plt.show() 

enter image description here

相關問題