Story paths on a map using a base map

import numpy as np data = np.loadtxt('path-tracks.csv',dtype=np.str,delimiter=',',skiprows=1) print data [['19.70' '-95.20' '2/5/04 6:45 AM' '1' '-38' 'CCM'] ['19.70' '-94.70' '2/5/04 7:45 AM' '1' '-48' 'CCM'] ['19.30' '-93.90' '2/5/04 8:45 AM' '1' '-60' 'CCM'] ['19.00' '-93.50' '2/5/04 9:45 AM' '1' '-58' 'CCM'] ['19.00' '-92.80' '2/5/04 10:45 AM' '1' '-50' 'CCM'] ['19.20' '-92.60' '2/5/04 11:45 AM' '1' '-40' 'CCM'] ['19.90' '-93.00' '2/5/04 12:45 PM' '1' '-43' 'CCM'] ['20.00' '-92.80' '2/5/04 1:15 PM' '1' '-32' 'CCM'] ['23.10' '-100.20' '30/5/04 4:45 AM' '2' '-45' 'SCME'] ['23.20' '-100.00' '30/5/04 5:45 AM' '2' '-56' 'SCME'] ['23.30' '-100.00' '30/5/04 6:45 AM' '2' '-48' 'SCME'] ['23.30' '-100.20' '30/5/04 7:45 AM' '2' '-32' 'SCME'] ['23.40' '-99.00' '31/5/04 3:15 AM' '3' '-36' 'SCM'] ['23.50' '-98.90' '31/5/04 4:15 AM' '3' '-46' 'SCM'] ['23.60' '-98.70' '31/5/04 5:15 AM' '3' '-68' 'SCM'] ['23.70' '-98.80' '31/5/04 6:15 AM' '3' '-30' 'SCM']] 

with the above code, I get an array whose columns represent: [Lat, Lon, Date, Identifier, Temperatures, Category]. Now I will put the code that will allow me to build the first and second columns on the map of Mexico:

 #!/usr/bin/python #Project Storm: Plot trajectories of convective systems #import libraries import numpy as np from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as pl # Plot a map for Mexico m = Basemap(projection='cyl', llcrnrlat=12, urcrnrlat=35,llcrnrlon=-120, urcrnrlon=-80, resolution='c', area_thresh=1000.) m.bluemarble() m.drawcoastlines(linewidth=0.5) m.drawcountries(linewidth=0.5) m.drawstates(linewidth=0.5) #Draw parallels and meridians m.drawparallels(np.arange(10.,35.,5.)) m.drawmeridians(np.arange(-120.,-80.,10.)) m.drawmapboundary(fill_color='aqua') #Open file whit numpy data = np.loadtxt('path-tracks.csv', dtype=np.str,delimiter=',', skiprows=1) latitude = data[:,0] longitude = data[:,1] #Convert latitude and longitude to coordinates X and Y x, y = m(longitude, latitude) #Plot the points on the map pl.plot(x,y,'ro-') pl.show() 

Points mapped to three different paths. Mi The ultimate idea is to draw a line connecting the points associated with each path. How can i do this?

can I draw an id or label for each path?

How can I set the size of the shape so that it can distinguish between the dots?

+4
source share
2 answers

You can set the size of a figure by simply creating a figure before calling Basemap . I used Pandas to read CSV because it makes it easy to group (along each path). If you don't want to use Pandas, you can probably get the same result by going through np.unique('cat') or something like that. If you use datetime as an index in Pandas, your points are automatically sorted by time in case your CSV is unsorted.

I'm not sure what you mean by id pattern. Legend allows you to distinguish paths, but you can also display 'Cat' on the map at the beginning or end of a line, for example.

The properties of your map make it a little “reduced" for such small paths. Using ax = pl.gca() and ax.set_xlim() , you can set the bounding box in mapcoordinates. Which you can get from the maximum and minimum coordinates in your paths + some buffer.

 import numpy as np from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as pl import pandas as pd fig = plt.figure(figsize=(12,12)) # Plot a map for Mexico m = Basemap(projection='cyl', llcrnrlat=12, urcrnrlat=35,llcrnrlon=-120, urcrnrlon=-80, resolution='c', area_thresh=1000.) m.bluemarble() m.drawcoastlines(linewidth=0.5) m.drawcountries(linewidth=0.5) m.drawstates(linewidth=0.5) #Draw parallels and meridians m.drawparallels(np.arange(10.,35.,5.)) m.drawmeridians(np.arange(-120.,-80.,10.)) m.drawmapboundary(fill_color='aqua') colors = {'CCM': 'red', 'SCME': 'white', 'SCM': 'yellow'} for cat, track in df.groupby('Cat'): latitude = track.Lat.values longitude = track.Lon.values #Convert latitude and longitude to coordinates X and Y x, y = m(longitude, latitude) #Plot the points on the map pl.plot(x,y,'-', label=cat, color=colors[cat]) lg = pl.legend() lg.get_frame().set_facecolor('grey') 

enter image description here

+6
source

Thank you for your ideas, I leave the correct code for constructing the trajectories of any object, in my case I drew the trajectories of convective storms.

 import numpy as np from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt fig = plt.figure(figsize=(12,12)) ax = fig.add_axes([0.1,0.1,0.8,0.8]) m = Basemap(projection='cyl', llcrnrlat=12, urcrnrlat=35,llcrnrlon=-120, urcrnrlon=-80, resolution='c', area_thresh=1000.) m.bluemarble() m.drawcoastlines(linewidth=0.5) m.drawcountries(linewidth=0.5) m.drawstates(linewidth=0.5) 

--- Dibujamos paralelos y meridianos ---

 m.drawparallels(np.arange(10.,35.,5.),labels=[1,0,0,1]) m.drawmeridians(np.arange(-120.,-80.,5.),labels=[1,0,0,1]) m.drawmapboundary(fill_color='aqua') 

--- Abrimos el archivo que contiene los datos ---

 import pandas as pd df = pd.read_csv('scm-2004.csv') for evento, group in df.groupby(['evento']): latitude = group.lat.values longitude = group.lon.values x,y = m(longitude, latitude) plt.plot(x,y,'y-',linewidth=2 ) plt.xlabel('Longitud') plt.ylabel('Latitud') plt.title('Trayectorias de Sistemas Convectivos 2004') plt.savefig('track-2004.jpg', dpi=100) 

With the code above, I get the desired number: 60 tracks drawn on a map of Mexico. I have only one last question: how to indicate the beginning of each of the storms, someone has an idea, how can I do this?

When I told him to write “Longitud” along the X axis (xlabel = 'Latitude'), overwrites the word by length values, how can I write a word under latitude values?

+2
source

All Articles