Plot multiple lines in python/basemap
-
22-06-2021 - |
Question
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
Solution
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow