Plotting list of lists in a same graph in Python
<p>I am trying to plot <code>(x,y)</code> where as <code>y = [[1,2,3],[4,5,6],[7,8,9]]</code>. </p>

<p>Say, <code>len(x) = len(y[1]) = len(y[2])</code>..
The length of the y is decided by the User input. I want to plot multiple plots of y in the same graph i.e, <code>(x, y[1],y[2],y[3],...)</code>. When I tried using loop it says <code>dimension error</code>.</p>

<p>I also tried: <code>plt.plot(x,y[i] for i in range(1,len(y)))</code> </p>

<p>How do I plot ? Please help.</p>

<pre><code>for i in range(1,len(y)):
plt.plot(x,y[i],label = 'id %s'%i)
plt.legend()
plt.show()
</code></pre>

<p>Assuming some sample values for x below is the code that could give you the desired output.</p>

<pre><code>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)):
    plt.plot(x,[pt[i] for pt in y],label = 'id %s'%i)
plt.legend()
plt.show()
</code></pre>

<p>Assumptions: <code>x</code> and any element in <code>y</code> are of the same length.
The idea is reading element by element so as to construct the list <code>(x,y[0]'s)</code>, <code>(x,y[1]'s)</code> and <code>(x,y[n]'s</code></p>

<p>Below is the plot I get for this case:
<a href="https://i.stack.imgur.com/0wvlq.png" rel="nofollow"><img src="https://i.stack.imgur.com/0wvlq.png" alt="Sample plot"></a></p>

python matplotlib nested-lists