MatPlotLib在同一行上打印每个oth旁边的图形

2024-04-19 09:34:22 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我正在制作一个程序,读取多个二维列表,并将它们绘制为步进图函数。我想像这样并排打印出每一组图形(我把图形做成不同的颜色只是为了区分这两种颜色):

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()

Tags: inimport图形foroutputdata颜色def
1条回答
网友
1楼 · 发布于 2024-04-19 09:34:22

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

相关问题 更多 >