title: Plot line from GPS points
body: <p>I have around 100 list of gps coordinates and I want to draw the line that each of those lists make.</p>
  
  <p>One of the lists plotted using scatter, look somewhat like this:</p>
  
  <p><img src="https://i.stack.imgur.com/OJp8C.png" alt="gps points"></p>
  
  <p>Clearly there's a line there;</p>
  
  <p>I tried several methods to sort the gps positions and plot them:</p>
  
  <pre><code>lats = []
  lngs = []
  with open(filename) as f:
      for line in f:
          lat, lng = line.split("\t")
          lats.append(float(lat))
          lngs.append(float(lng))
  
  def sort_positions(position):
      return position[0]+position[1]
  
  positions= zip(lngs, lats)
  positions = sorted(poss, key=sort_positions)
  for i, positionin enumerate(positions):
      lng, lat = position
      #plt.scatter(lng, lat)
      try:
          n_lng, n_lat = positions[i+1]
          plt.plot((lng, n_lng),(lat, n_lat), "b")
      except IndexError:
          pass
  
  plt.show()
  </code></pre>
  
  <h3>Sorting by Longitude</h3>
  
  <pre><code>def sort_positions(position):
      return position[0]
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/CQvxc.png" alt="Longitude Sort"></p>
  
  <h3>Sorting by Latitude</h3>
  
  <pre><code>def sort_positions(position):
      return position[1]
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/Mkilh.png" alt="Latitude Sort"></p>
  
  <h3>Sorting by Summing Both</h3>
  
  <pre><code>def sort_positions(position):
      return position[0]+position[1]
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/olAQu.png" alt="Sommation Sort"></p>
  
  <p>If the line are mostly straight in one of the axis(latitude/longitude), it plots fine (some minor bumps are still showing)</p>
  
  <p>Here's one of the lists that do plot fine, sorting by latitude.</p>
  
  <p><img src="https://i.stack.imgur.com/yirRM.png" alt="Latitude Sort 2"></p>
  
  <p>I to only plot if the distance between the two points was lesser than 200~500 meters, but I endup with holes in the lines, since there's some missing data.</p>
  
  <p><img src="https://i.stack.imgur.com/HGZbq.png" alt="Distance"></p>
  
  <p>Probably I'm doing this wrong. Does anyone know how to plot this lines properly?</p>
  
  <h2>EDIT:</h2>
  
  <p>In response to <code>rth</code> answer:</p>
  
  <p><img src="https://i.stack.imgur.com/3qYyu.png" alt="Two Approachs"></p>
  
  <p>The blue line is using his approach in this questions, the red one is using the method in his <a href="https://stackoverflow.com/questions/31464345/fitting-a-closed-curve-to-points/31466013#31466013">other answer</a>.</p>
  
  <p>Ignoring the fact that the red is closing the loop.</p>
  
  <p>There are some limitations in both, first, the red one can't handle too many points, I had to to use 1/90 of the points for both, the blue one generates some weird sharp turns when the points are too clustered (the dark spots in the image), while the red one some weird curves in those places.</p>