簡體   English   中英

MatPlotLib在彼此相鄰的同一行上打印出圖形

[英]MatPlotLib printing out graphs on the same line next to each other

因此,我正在編寫一個程序,該程序可以讀取多個二維列表並將其繪制為階梯圖函數。 我想像這樣並排打印每組圖(我使圖具有不同的顏色,只是為了區分兩者):

期望的輸出

但是現在我的代碼使這兩個集合彼此重疊,如下所示:

實際產量

我相信這可能與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()

您可以向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()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM