Search code examples
pythonmatplotlibsubplot

Plot lists in different subplots


I have data in lists that I want to plot, which looks like this:

time = [1633093403.754783918, 1633093403.755350983, 1633093403.760918965, 1633093403.761298577, 1633093403.761340378, 1633093403.761907443]
data = [[0,1], [1,1], [0,0], [1,0], [0,1], [1,1]]

Now I want to plot the first element of each list in data in a subplot and the second one in another one. So, all elements in data[n][0] in one plot and data[n][1] in another one.

Thanks for the help.


Solution

  • Use list comprehension:

    time = [1633093403.754783918, 1633093403.755350983, 1633093403.760918965, 1633093403.761298577, 1633093403.761340378, 1633093403.761907443]
    data = [[0,1], [1,1], [0,0], [1,0], [0,1], [1,1]]
    
    
    data_0 = [i[0] for i in data]
    data_1 = [i[1] for i in data]
    
    fig, (ax1, ax2) = plt.subplots(1, 2)
    ax1.plot(time, data_0)
    ax2.plot(time, data_1)
    plt.show()