Plotting list of lists in a same graph in Python
Use a for loop to generate the plots and use the .show()
method after the for loop.
import matplotlib.pyplot as plt
for impacts in impactData:
timefilteredForce = plt.plot(impacts)
timefilteredForce = plt.xlabel('points')
timefilteredForce = plt.ylabel('Force')
plt.show()
impactData is a list of lists.
Assuming some sample values for x, below is the code that could give you the desired output.
import matplotlib.pyplot as plt
x = [1,2,3]
y = [[1,2,3],[4,5,6],[7,8,9]]
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("A test graph")
for i in range(len(y[0])):
plt.plot(x,[pt[i] for pt in y],label = 'id %s'%i)
plt.legend()
plt.show()
Assumptions: x
and any element in y
are of the same length.
The idea is reading element by element so as to construct the list (x,y[0]'s)
, (x,y[1]'s)
and (x,y[n]'s
.
Edited: Adapt the code if y
contains more lists.
Below is the plot I get for this case: