简体   繁体   中英

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

So I am making a program that reads in multiple two dimensional lists and plots them as step graph functions. I want to print out each set of graphs side by side like so (I made the graphs different colors just to differentiate the two):

Desired Output

However my code right now makes these two sets overlap each other instead, like so:

Actual Output

I believe it might have something to do with my "t" variable in plotPoints but I am not sure what I need to do. Any help would be greatly appreciated.

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

You can add some offset to 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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM