4

I have just started learning python/matplotlib/basemap and could really use some help. How do you plot multiple lines?

Say my data looks something like:

[(lat1,lon1) (lat2,lon2) (lat3,lon3)]
[(lat1,lon1) (lat2,lon2) (lat3,lon3)]
[(lat1,lon1) (lat2,lon2) (lat3,lon3)]
...

I want to plot a separate line for each line in my data. What I get with my code, however, is that is connects the last point in the previous line to the first in the current line. Can anyone help me fix this? Thank you!

EDIT: Here is what I have for code:

for page in files:
    file = open(dir + '/' + page)
    for line in file:
       lines = line.split()
       time = lines[0]
       lon = lines[1]
       lat = lines[2]
       lon_float = float(lon)
       lat_float = float(lat)
       lats.append(lat_float)
       lons.append(lon_float)
    x,y = m(lons, lats)
    m.plot(x,y,'D-')
plt.show()

I want to plot one line for every file (which has multiple lat/long pairs) Also, m is my Baseplot instance

1
  • ah, see my edited answer. We were barking up the wrong tree;) Commented Jul 22, 2012 at 21:46

1 Answer 1

2

You are not clearing lats and lons so every time through the file loop you are accumulating the points.

for page in files:
    file = open(dir + '/' + page)
    lats = []
    lons = []
    for line in file:
        ...

EDIT: Completely re-wrote answer

Sign up to request clarification or add additional context in comments.

2 Comments

To help with clarity, the zip statements could be split into two lines: lats, lons = zip(*data) followed by plt.plot(lons, lats). I think the order of the lons and lats may need to be switched for @tcaswells example to work (but I can never remember with confidence).
Yes! That is exactly what I wanted! Thank you so much!! That make so much sense!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.