2017-11-09 2 views
0

그래서 여러 개의 2 차원 목록을 읽고 단계 그래프 기능으로 그려주는 프로그램을 만들고 있습니다. 내가 지금과 같은 그래프를 나란히의 각 세트를 인쇄 할 (난 그냥 구별하는 그래프 서로 다른 색상을 만들어 두) :MatPlotLib 서로 옆에 같은 줄에 그래프를 인쇄합니다.

Desired Output

그러나 내 코드는 지금이 두 세트 대신 서로 중복한다 그래서 같은 :

Actual Output

나는 plotPoints 내 "t"변수 함께 할 수있는 뭔가가있을 수 있습니다 생각하지만 난 무엇을해야하는지 모르겠습니다. 어떤 도움이라도 대단히 감사하겠습니다.

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


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

     plt.step(t, data + i * 3, linewidth=1.5, where='post', color=color) 


     # Labels the graphs with binary sequence 
     for tbit, bit in enumerate(bits[i]): 
      plt.text(tbit + 0.3, 0.1 + i * 3, str(bit), fontsize=6, color=color) 


def main(): 

    plt.ylim([-1, 32]) 

    set1 = [[0, 0, 0, 1, 1, 0, 1, 1], [0, 0, 1, 0, 1, 1, 0, 0], [1, 1, 0, 0, 1, 0, 0, 0]] 
    set2 = [[1, 1, 1, 0, 0, 1, 0, 0], [1, 1, 0, 1, 0, 0, 1, 1], [0, 0, 1, 1, 0, 1, 1, 1]] 


    plotPoints(set1, 'g') 
    plotPoints(set2, 'b') 


    # removes the built in graph axes and prints line every interation 
    plt.gca().axis('off') 
    plt.ylim([-1, 10]) 


    plt.show() 

main() 

답변

1

당신은 어떤 t 오프셋을 추가 할 수 있습니다.

import matplotlib.pyplot as plt 
import numpy as np 


def plotPoints(bits, color, offset=0): 
    for i in range(len(bits)): 
     data = np.repeat(bits[i], 2) 
     t = 0.5 * np.arange(len(data)) + offset 

     plt.step(t, data + i * 3, linewidth=1.5, where='post', color=color) 


     # Labels the graphs with binary sequence 
     for tbit, bit in enumerate(bits[i]): 
      plt.text(tbit + 0.3 +offset, 0.1 + i * 3, str(bit), fontsize=6, color=color) 


def main(): 

    set1 = [[0, 0, 0, 1, 1, 0, 1, 1], [0, 0, 1, 0, 1, 1, 0, 0], [1, 1, 0, 0, 1, 0, 0, 0]] 
    set2 = [[1, 1, 1, 0, 0, 1, 0, 0], [1, 1, 0, 1, 0, 0, 1, 1], [0, 0, 1, 1, 0, 1, 1, 1]] 


    plotPoints(set1, 'g') 
    plotPoints(set2, 'b', offset=len(set1[0])) 


    # removes the built in graph axes and prints line every interation 
    plt.gca().axis('off') 
    plt.ylim([-1, 10]) 


    plt.show() 

main() 

enter image description here