Connect data points in the order they were individually plotted - matplotlib
<p>I am trying to connect points which are plotted individually, but I want to connect them in the order they are plotted. I have a list of numbers referring to a set of points (ordered pairs). The list tells what order the points should be plotted in.</p>

<p>The points are in a dictionary <code>P</code>. So let's say <code>P[1]</code>, <code>P[2]</code>, <code>P[3]</code>.  Then <code>list = (2, 1, 3)</code>.  I want to plot it so that a line connects <code>P[2]</code> to <code>P[1]</code> to <code>P[3]</code>.  Where <code>P[i] = (x, y)</code> is some tuplet with x-coordinate <code>x</code> and y-coordinate <code>y</code>.</p>

<p>As I have it now, I am plotting the points using a loop.  This works, but it doesn't connect the points.  Any help would be great!  Thanks.  </p>

<pre><code>x=[-4, 2, -1, 5]
y=[-3, -2, 4, 2]
n = 3 #number of cities

P = dict()
P_0 = (x[0],y[0])

for i in range (1,n):
    P[i] = (x[i],y[i])

route = (2,1,3)
plt.plot([P_0[0]],[P_0[1]])
for k in range (0,n-1):
    plt.plot([P[route[k]][0]],[P[route[k]][1]])
</code></pre>