From: Michael R. <raw...@ya...> - 2011-04-19 20:09:31
|
I'm trying to plot a series of points/locations on a map. I'm reading the latitudes and longitudes from a file, with each lat, lon pair on each record (line). Here is the code: def make_float(line): lati, longi = line.split() return float(lati), float(longi) my_dict = {} with open("file.txt") as f: for item in f: lati,longi = make_float(item) my_dict[lati] = longi xpt,ypt = m(-76.1670,39.4670 ) plt.text(xpt,ypt,'1',color='white') #print my_dict The matplotlib code which I've previously used to plot a single point on the map is below, with longitude and latitude in ( ): xpt,ypt = m(-70.758392,42.960445) plt.text(xpt,ypt,'1',color='white') When replacing (-70.758392,42.960445) with (longi,lati), the code plots only a single '1' at the location of just the last coordinate pair in the file. So now I only need to plot them all. Does the code I've implemented have an implicit loop to it? Mike |
From: Ian B. <ib...@pu...> - 2011-04-19 22:53:01
|
To clarify, you are trying to read in a set of (lat,lon) points in a file that is space delimited, store the data, and then put a text marker at each point, with each point numbered in order? The critical part is that you want to use a list (or numpy array) instead of a dictionary. Something like this ought to do (don't have MPL on this computer though - pretty sure this should work): lines=open('file.txt','r').readlines() (lats,lons)=([],[]) for line in lines: (lat,lon)=line.strip().split(' ') lats.append(float(lat)) lons.append(float(lon)) for i in range(len(lons)): plt.text(lats[i],lon[i],str(i+1),ha='center',va='center',color='white') I'm sure there are a bunch of more compact ways to do this, but this should work. Ian ---- Ian Bell Graduate Research Assistant Herrick Labs Purdue University email: ib...@pu... cell: (607)227-7626 On Tue, Apr 19, 2011 at 4:09 PM, Michael Rawlins <raw...@ya...>wrote: > > I'm trying to plot a series of points/locations on a map. I'm reading the > latitudes and longitudes from a file, with each lat, lon pair on each record > (line). Here is the code: > > def make_float(line): > lati, longi = line.split() > return float(lati), float(longi) > > my_dict = {} > with open("file.txt") as f: > for item in f: > lati,longi = make_float(item) > my_dict[lati] = longi > > xpt,ypt = m(-76.1670,39.4670 ) > plt.text(xpt,ypt,'1',color='white') > > #print my_dict > > The matplotlib code which I've previously used to plot a single point on > the map is below, with longitude and latitude in ( ): > > xpt,ypt = m(-70.758392,42.960445) > plt.text(xpt,ypt,'1',color='white') > > When replacing (-70.758392,42.960445) with (longi,lati), the code plots > only a single '1' at the location of just the last coordinate pair in the > file. So now I only need to plot them all. Does the code I've implemented > have an implicit loop to it? > > Mike > > > > > ------------------------------------------------------------------------------ > Benefiting from Server Virtualization: Beyond Initial Workload > Consolidation -- Increasing the use of server virtualization is a top > priority.Virtualization can reduce costs, simplify management, and improve > application availability and disaster protection. Learn more about boosting > the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > |
From: Michael R. <raw...@ya...> - 2011-04-19 23:14:59
|
Yes, there is whitespace between each lat and lon on each line. But, actually, I'd simply like to plot a dot at each location. The '1' was there in my example because I do not yet know how to plot a particular symbol. Here is what I got when I tried the code you just suggested. Traceback (most recent call last): File "test.py", line 319, in <module> (lat,lon)=line.strip().split(' ') ValueError: too many values to unpack There are 203 records in the data file. Line 319 of test.py is this: (lat,lon)=line.strip().split(' ') --- On Tue, 4/19/11, Ian Bell <ib...@pu...> wrote: From: Ian Bell <ib...@pu...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "Michael Rawlins" <raw...@ya...> Cc: Mat...@li... Date: Tuesday, April 19, 2011, 6:52 PM To clarify, you are trying to read in a set of (lat,lon) points in a file that is space delimited, store the data, and then put a text marker at each point, with each point numbered in order? The critical part is that you want to use a list (or numpy array) instead of a dictionary. Something like this ought to do (don't have MPL on this computer though - pretty sure this should work): lines=open('file.txt','r').readlines() (lats,lons)=([],[]) for line in lines: (lat,lon)=line.strip().split(' ') lats.append(float(lat)) lons.append(float(lon)) for i in range(len(lons)): plt.text(lats[i],lon[i],str(i+1),ha='center',va='center',color='white') I'm sure there are a bunch of more compact ways to do this, but this should work. Ian ---- Ian Bell Graduate Research Assistant Herrick Labs Purdue University email: ib...@pu... cell: (607)227-7626 On Tue, Apr 19, 2011 at 4:09 PM, Michael Rawlins <raw...@ya...> wrote: I'm trying to plot a series of points/locations on a map. I'm reading the latitudes and longitudes from a file, with each lat, lon pair on each record (line). Here is the code: def make_float(line): lati, longi = line.split() return float(lati), float(longi) my_dict = {} with open("file.txt") as f: for item in f: lati,longi = make_float(item) my_dict[lati] = longi xpt,ypt = m(-76.1670,39.4670 ) plt.text(xpt,ypt,'1',color='white') #print my_dict The matplotlib code which I've previously used to plot a single point on the map is below, with longitude and latitude in ( ): xpt,ypt = m(-70.758392,42.960445) plt.text(xpt,ypt,'1',color='white') When replacing (-70.758392,42.960445) with (longi,lati), the code plots only a single '1' at the location of just the last coordinate pair in the file. So now I only need to plot them all. Does the code I've implemented have an implicit loop to it? Mike ------------------------------------------------------------------------------ Benefiting from Server Virtualization: Beyond Initial Workload Consolidation -- Increasing the use of server virtualization is a top priority.Virtualization can reduce costs, simplify management, and improve application availability and disaster protection. Learn more about boosting the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev _______________________________________________ Matplotlib-users mailing list Mat...@li... https://lists.sourceforge.net/lists/listinfo/matplotlib-users |
From: Ian B. <ib...@pu...> - 2011-04-19 23:22:32
|
If you want to plot a given marker at the point, for instance a circle, replace the last line of my code plt.text...... with plt.plot(lats,lons,'o') for a circle, or plt.plot(lats,lons,'s') for a square. Refer to Plot<http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot>for more information on the markers you can use. You are getting the error because you have a delimiter different than a single space, so it isn't splitting the line. Replace ' ' in the split command with your whitespace delimiter. Is it a tab? Then you want '\t' . Good luck, Ian ---- Ian Bell Graduate Research Assistant Herrick Labs Purdue University email: ib...@pu... cell: (607)227-7626 On Tue, Apr 19, 2011 at 7:14 PM, Michael Rawlins <raw...@ya...>wrote: > > Yes, there is whitespace between each lat and lon on each line. But, > actually, I'd simply like to plot a dot at each location. The '1' was there > in my example because I do not yet know how to plot a particular symbol. > Here is what I got when I tried the code you just suggested. > > Traceback (most recent call last): > File "test.py", line 319, in <module> > > (lat,lon)=line.strip().split(' ') > ValueError: too many values to unpack > > > There are 203 records in the data file. Line 319 of test.py is this: > > > (lat,lon)=line.strip().split(' ') > > > --- On *Tue, 4/19/11, Ian Bell <ib...@pu...>* wrote: > > > From: Ian Bell <ib...@pu...> > Subject: Re: [Matplotlib-users] plotting points/locations from data file > To: "Michael Rawlins" <raw...@ya...> > Cc: Mat...@li... > Date: Tuesday, April 19, 2011, 6:52 PM > > > To clarify, you are trying to read in a set of (lat,lon) points in a file > that is space delimited, store the data, and then put a text marker at each > point, with each point numbered in order? The critical part is that you > want to use a list (or numpy array) instead of a dictionary. Something like > this ought to do (don't have MPL on this computer though - pretty sure this > should work): > > lines=open('file.txt','r').readlines() > (lats,lons)=([],[]) > for line in lines: > (lat,lon)=line.strip().split(' ') > lats.append(float(lat)) > lons.append(float(lon)) > > for i in range(len(lons)): > plt.text(lats[i],lon[i],str(i+1),ha='center',va='center',color='white') > > I'm sure there are a bunch of more compact ways to do this, but this should > work. > > Ian > ---- > Ian Bell > Graduate Research Assistant > Herrick Labs > Purdue University > email: ib...@pu... <http://mc/compose?to=ib...@pu...> > cell: (607)227-7626 > > > On Tue, Apr 19, 2011 at 4:09 PM, Michael Rawlins <raw...@ya...<http://mc/compose?to=raw...@ya...> > > wrote: > > > I'm trying to plot a series of points/locations on a map. I'm reading the > latitudes and longitudes from a file, with each lat, lon pair on each record > (line). Here is the code: > > def make_float(line): > lati, longi = line.split() > return float(lati), float(longi) > > my_dict = {} > with open("file.txt") as f: > for item in f: > lati,longi = make_float(item) > my_dict[lati] = longi > > xpt,ypt = m(-76.1670,39.4670 ) > plt.text(xpt,ypt,'1',color='white') > > #print my_dict > > The matplotlib code which I've previously used to plot a single point on > the map is below, with longitude and latitude in ( ): > > xpt,ypt = m(-70.758392,42.960445) > plt.text(xpt,ypt,'1',color='white') > > When replacing (-70.758392,42.960445) with (longi,lati), the code plots > only a single '1' at the location of just the last coordinate pair in the > file. So now I only need to plot them all. Does the code I've implemented > have an implicit loop to it? > > Mike > > > > > ------------------------------------------------------------------------------ > Benefiting from Server Virtualization: Beyond Initial Workload > Consolidation -- Increasing the use of server virtualization is a top > priority.Virtualization can reduce costs, simplify management, and improve > application availability and disaster protection. Learn more about boosting > the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev > _______________________________________________ > Matplotlib-users mailing list > Mat...@li...<http://mc/compose?to=Mat...@li...> > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > > |
From: Michael R. <raw...@ya...> - 2011-04-19 23:27:07
|
Sorry I should have mentioned that longitudes are negative; there is a '-' before each longitude, like so: 39.4670 -76.1670 46.4000 -74.7670 45.3830 -75.7170 43.6170 -79.3830 45.5170 -73.4170 Also the plt.text line you sent had lon[i] rather than lons[i]. I corrected that and changed my longitudes to not have the '-' sign and the code ran without error. Could the '-' be causing a problem? I need to input the lat, lon as in the file as shown above. Mike --- On Tue, 4/19/11, Ian Bell <ib...@pu...> wrote: From: Ian Bell <ib...@pu...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "Michael Rawlins" <raw...@ya...> Cc: Mat...@li... Date: Tuesday, April 19, 2011, 7:22 PM If you want to plot a given marker at the point, for instance a circle, replace the last line of my code plt.text...... with plt.plot(lats,lons,'o') for a circle, or plt.plot(lats,lons,'s') for a square. Refer to Plot for more information on the markers you can use. You are getting the error because you have a delimiter different than a single space, so it isn't splitting the line. Replace ' ' in the split command with your whitespace delimiter. Is it a tab? Then you want '\t' . Good luck, Ian ---- Ian Bell Graduate Research Assistant Herrick Labs Purdue University email: ib...@pu... cell: (607)227-7626 On Tue, Apr 19, 2011 at 7:14 PM, Michael Rawlins <raw...@ya...> wrote: Yes, there is whitespace between each lat and lon on each line. But, actually, I'd simply like to plot a dot at each location. The '1' was there in my example because I do not yet know how to plot a particular symbol. Here is what I got when I tried the code you just suggested. Traceback (most recent call last): File "test.py", line 319, in <module> (lat,lon)=line.strip().split(' ') ValueError: too many values to unpack There are 203 records in the data file. Line 319 of test.py is this: (lat,lon)=line.strip().split(' ') --- On Tue, 4/19/11, Ian Bell <ib...@pu...> wrote: From: Ian Bell <ib...@pu...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "Michael Rawlins" <raw...@ya...> Cc: Mat...@li... Date: Tuesday, April 19, 2011, 6:52 PM To clarify, you are trying to read in a set of (lat,lon) points in a file that is space delimited, store the data, and then put a text marker at each point, with each point numbered in order? The critical part is that you want to use a list (or numpy array) instead of a dictionary. Something like this ought to do (don't have MPL on this computer though - pretty sure this should work): lines=open('file.txt','r').readlines() (lats,lons)=([],[]) for line in lines: (lat,lon)=line.strip().split(' ') lats.append(float(lat)) lons.append(float(lon)) for i in range(len(lons)): plt.text(lats[i],lon[i],str(i+1),ha='center',va='center',color='white') I'm sure there are a bunch of more compact ways to do this, but this should work. Ian ---- Ian Bell Graduate Research Assistant Herrick Labs Purdue University email: ib...@pu... cell: (607)227-7626 On Tue, Apr 19, 2011 at 4:09 PM, Michael Rawlins <raw...@ya...> wrote: I'm trying to plot a series of points/locations on a map. I'm reading the latitudes and longitudes from a file, with each lat, lon pair on each record (line). Here is the code: def make_float(line): lati, longi = line.split() return float(lati), float(longi) my_dict = {} with open("file.txt") as f: for item in f: lati,longi = make_float(item) my_dict[lati] = longi xpt,ypt = m(-76.1670,39.4670 ) plt.text(xpt,ypt,'1',color='white') #print my_dict The matplotlib code which I've previously used to plot a single point on the map is below, with longitude and latitude in ( ): xpt,ypt = m(-70.758392,42.960445) plt.text(xpt,ypt,'1',color='white') When replacing (-70.758392,42.960445) with (longi,lati), the code plots only a single '1' at the location of just the last coordinate pair in the file. So now I only need to plot them all. Does the code I've implemented have an implicit loop to it? Mike ------------------------------------------------------------------------------ Benefiting from Server Virtualization: Beyond Initial Workload Consolidation -- Increasing the use of server virtualization is a top priority.Virtualization can reduce costs, simplify management, and improve application availability and disaster protection. Learn more about boosting the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev _______________________________________________ Matplotlib-users mailing list Mat...@li... https://lists.sourceforge.net/lists/listinfo/matplotlib-users |
From: Ian B. <ib...@pu...> - 2011-04-19 23:32:54
|
The easiest solution would be to put a comma as the delimiter between the lat and lon, and then change split(' ') to split(','). Then everything should work fine. I exclusively work with comma separated files for this exact reason. You are right that I had a typo, it should be lons[i]. It looks like you have two spaces as the delimiter currently based on your copy-paste. That's why split doesn't give you two values. In general I recommend that you avoid two spaces as the delimiter, just going to cause problems. ---- Ian Bell Graduate Research Assistant Herrick Labs Purdue University email: ib...@pu... cell: (607)227-7626 On Tue, Apr 19, 2011 at 7:26 PM, Michael Rawlins <raw...@ya...>wrote: > > Sorry I should have mentioned that longitudes are negative; there is a '-' > before each longitude, like so: > > 39.4670 -76.1670 > 46.4000 -74.7670 > 45.3830 -75.7170 > 43.6170 -79.3830 > 45.5170 -73.4170 > > > Also the plt.text line you sent had lon[i] rather than lons[i]. I > corrected that and changed my longitudes to not have the '-' sign and the > code ran without error. Could the '-' be causing a problem? I need to input > the lat, lon as in the file as shown above. > > Mike > > > --- On *Tue, 4/19/11, Ian Bell <ib...@pu...>* wrote: > > > From: Ian Bell <ib...@pu...> > Subject: Re: [Matplotlib-users] plotting points/locations from data file > To: "Michael Rawlins" <raw...@ya...> > Cc: Mat...@li... > Date: Tuesday, April 19, 2011, 7:22 PM > > > If you want to plot a given marker at the point, for instance a circle, > replace the last line of my code plt.text...... with > > plt.plot(lats,lons,'o') > > for a circle, or > > plt.plot(lats,lons,'s') > > for a square. Refer to Plot<http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot>for more information on the markers you can use. You are getting the error > because you have a delimiter different than a single space, so it isn't > splitting the line. Replace ' ' in the split command with your whitespace > delimiter. Is it a tab? Then you want '\t' . > > Good luck, > Ian > > ---- > Ian Bell > Graduate Research Assistant > Herrick Labs > Purdue University > email: ib...@pu... <http://mc/compose?to=ib...@pu...> > cell: (607)227-7626 > > > On Tue, Apr 19, 2011 at 7:14 PM, Michael Rawlins <raw...@ya...<http://mc/compose?to=raw...@ya...> > > wrote: > > > Yes, there is whitespace between each lat and lon on each line. But, > actually, I'd simply like to plot a dot at each location. The '1' was there > in my example because I do not yet know how to plot a particular symbol. > Here is what I got when I tried the code you just suggested. > > Traceback (most recent call last): > File "test.py", line 319, in <module> > > (lat,lon)=line.strip().split(' ') > ValueError: too many values to unpack > > > There are 203 records in the data file. Line 319 of test.py is this: > > > (lat,lon)=line.strip().split(' ') > > > --- On *Tue, 4/19/11, Ian Bell <ib...@pu...<http://mc/compose?to=ib...@pu...> > >* wrote: > > > From: Ian Bell <ib...@pu... <http://mc/compose?to=ib...@pu...>> > Subject: Re: [Matplotlib-users] plotting points/locations from data file > To: "Michael Rawlins" <raw...@ya...<http://mc/compose?to=raw...@ya...> > > > Cc: Mat...@li...<http://mc/compose?to=Mat...@li...> > Date: Tuesday, April 19, 2011, 6:52 PM > > > To clarify, you are trying to read in a set of (lat,lon) points in a file > that is space delimited, store the data, and then put a text marker at each > point, with each point numbered in order? The critical part is that you > want to use a list (or numpy array) instead of a dictionary. Something like > this ought to do (don't have MPL on this computer though - pretty sure this > should work): > > lines=open('file.txt','r').readlines() > (lats,lons)=([],[]) > for line in lines: > (lat,lon)=line.strip().split(' ') > lats.append(float(lat)) > lons.append(float(lon)) > > for i in range(len(lons)): > plt.text(lats[i],lon[i],str(i+1),ha='center',va='center',color='white') > > I'm sure there are a bunch of more compact ways to do this, but this should > work. > > Ian > ---- > Ian Bell > Graduate Research Assistant > Herrick Labs > Purdue University > email: ib...@pu... <http://mc/compose?to=ib...@pu...> > cell: (607)227-7626 > > > On Tue, Apr 19, 2011 at 4:09 PM, Michael Rawlins <raw...@ya...<http://mc/compose?to=raw...@ya...> > > wrote: > > > I'm trying to plot a series of points/locations on a map. I'm reading the > latitudes and longitudes from a file, with each lat, lon pair on each record > (line). Here is the code: > > def make_float(line): > lati, longi = line.split() > return float(lati), float(longi) > > my_dict = {} > with open("file.txt") as f: > for item in f: > lati,longi = make_float(item) > my_dict[lati] = longi > > xpt,ypt = m(-76.1670,39.4670 ) > plt.text(xpt,ypt,'1',color='white') > > #print my_dict > > The matplotlib code which I've previously used to plot a single point on > the map is below, with longitude and latitude in ( ): > > xpt,ypt = m(-70.758392,42.960445) > plt.text(xpt,ypt,'1',color='white') > > When replacing (-70.758392,42.960445) with (longi,lati), the code plots > only a single '1' at the location of just the last coordinate pair in the > file. So now I only need to plot them all. Does the code I've implemented > have an implicit loop to it? > > Mike > > > > > ------------------------------------------------------------------------------ > Benefiting from Server Virtualization: Beyond Initial Workload > Consolidation -- Increasing the use of server virtualization is a top > priority.Virtualization can reduce costs, simplify management, and improve > application availability and disaster protection. Learn more about boosting > the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev > _______________________________________________ > Matplotlib-users mailing list > Mat...@li...<http://mc/compose?to=Mat...@li...> > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > > > |
From: Michael R. <raw...@ya...> - 2011-04-19 23:58:40
|
I've set up the data file with comma delimiter and one space. Split command now: (lat,lon)=line.strip().split(',') Code runs without error but no points on the map. These two lines, however, do produce an asterisk at the proper location: xpt,ypt = m(-75.0,43.0) text(xpt,ypt,'*') I will strip the code down to bare minimum (take out reading of netCDF data file) and paste it here along with ten example lats,lons. --- On Tue, 4/19/11, Ian Bell <ib...@pu...> wrote: From: Ian Bell <ib...@pu...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "Michael Rawlins" <raw...@ya...> Cc: Mat...@li... Date: Tuesday, April 19, 2011, 7:32 PM The easiest solution would be to put a comma as the delimiter between the lat and lon, and then change split(' ') to split(','). Then everything should work fine. I exclusively work with comma separated files for this exact reason. You are right that I had a typo, it should be lons[i]. It looks like you have two spaces as the delimiter currently based on your copy-paste. That's why split doesn't give you two values. In general I recommend that you avoid two spaces as the delimiter, just going to cause problems. ---- Ian Bell Graduate Research Assistant Herrick Labs Purdue University email: ib...@pu... cell: (607)227-7626 On Tue, Apr 19, 2011 at 7:26 PM, Michael Rawlins <raw...@ya...> wrote: Sorry I should have mentioned that longitudes are negative; there is a '-' before each longitude, like so: 39.4670 -76.1670 46.4000 -74.7670 45.3830 -75.7170 43.6170 -79.3830 45.5170 -73.4170 Also the plt.text line you sent had lon[i] rather than lons[i]. I corrected that and changed my longitudes to not have the '-' sign and the code ran without error. Could the '-' be causing a problem? I need to input the lat, lon as in the file as shown above. Mike --- On Tue, 4/19/11, Ian Bell <ib...@pu...> wrote: From: Ian Bell <ib...@pu...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "Michael Rawlins" <raw...@ya...> Cc: Mat...@li... Date: Tuesday, April 19, 2011, 7:22 PM If you want to plot a given marker at the point, for instance a circle, replace the last line of my code plt.text...... with plt.plot(lats,lons,'o') for a circle, or plt.plot(lats,lons,'s') for a square. Refer to Plot for more information on the markers you can use. You are getting the error because you have a delimiter different than a single space, so it isn't splitting the line. Replace ' ' in the split command with your whitespace delimiter. Is it a tab? Then you want '\t' . Good luck, Ian ---- Ian Bell Graduate Research Assistant Herrick Labs Purdue University email: ib...@pu... cell: (607)227-7626 On Tue, Apr 19, 2011 at 7:14 PM, Michael Rawlins <raw...@ya...> wrote: Yes, there is whitespace between each lat and lon on each line. But, actually, I'd simply like to plot a dot at each location. The '1' was there in my example because I do not yet know how to plot a particular symbol. Here is what I got when I tried the code you just suggested. Traceback (most recent call last): File "test.py", line 319, in <module> (lat,lon)=line.strip().split(' ') ValueError: too many values to unpack There are 203 records in the data file. Line 319 of test.py is this: (lat,lon)=line.strip().split(' ') --- On Tue, 4/19/11, Ian Bell <ib...@pu...> wrote: From: Ian Bell <ib...@pu...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "Michael Rawlins" <raw...@ya...> Cc: Mat...@li... Date: Tuesday, April 19, 2011, 6:52 PM To clarify, you are trying to read in a set of (lat,lon) points in a file that is space delimited, store the data, and then put a text marker at each point, with each point numbered in order? The critical part is that you want to use a list (or numpy array) instead of a dictionary. Something like this ought to do (don't have MPL on this computer though - pretty sure this should work): lines=open('file.txt','r').readlines() (lats,lons)=([],[]) for line in lines: (lat,lon)=line.strip().split(' ') lats.append(float(lat)) lons.append(float(lon)) for i in range(len(lons)): plt.text(lats[i],lon[i],str(i+1),ha='center',va='center',color='white') I'm sure there are a bunch of more compact ways to do this, but this should work. Ian ---- Ian Bell Graduate Research Assistant Herrick Labs Purdue University email: ib...@pu... cell: (607)227-7626 On Tue, Apr 19, 2011 at 4:09 PM, Michael Rawlins <raw...@ya...> wrote: I'm trying to plot a series of points/locations on a map. I'm reading the latitudes and longitudes from a file, with each lat, lon pair on each record (line). Here is the code: def make_float(line): lati, longi = line.split() return float(lati), float(longi) my_dict = {} with open("file.txt") as f: for item in f: lati,longi = make_float(item) my_dict[lati] = longi xpt,ypt = m(-76.1670,39.4670 ) plt.text(xpt,ypt,'1',color='white') #print my_dict The matplotlib code which I've previously used to plot a single point on the map is below, with longitude and latitude in ( ): xpt,ypt = m(-70.758392,42.960445) plt.text(xpt,ypt,'1',color='white') When replacing (-70.758392,42.960445) with (longi,lati), the code plots only a single '1' at the location of just the last coordinate pair in the file. So now I only need to plot them all. Does the code I've implemented have an implicit loop to it? Mike ------------------------------------------------------------------------------ Benefiting from Server Virtualization: Beyond Initial Workload Consolidation -- Increasing the use of server virtualization is a top priority.Virtualization can reduce costs, simplify management, and improve application availability and disaster protection. Learn more about boosting the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev _______________________________________________ Matplotlib-users mailing list Mat...@li... https://lists.sourceforge.net/lists/listinfo/matplotlib-users |
From: G J. <gle...@gm...> - 2011-04-19 23:32:51
|
You may find it easier to use mlab.csv2rec or numpy.loadtxt. e.g. data = csv2rec(filename,delimiter=' ') plot(data[:,0],data[:,1],'o') On Tue, Apr 19, 2011 at 4:26 PM, Michael Rawlins <raw...@ya...>wrote: > > Sorry I should have mentioned that longitudes are negative; there is a '-' > before each longitude, like so: > > 39.4670 -76.1670 > 46.4000 -74.7670 > 45.3830 -75.7170 > 43.6170 -79.3830 > 45.5170 -73.4170 > > > Also the plt.text line you sent had lon[i] rather than lons[i]. I > corrected that and changed my longitudes to not have the '-' sign and the > code ran without error. Could the '-' be causing a problem? I need to input > the lat, lon as in the file as shown above. > > Mike > > > --- On *Tue, 4/19/11, Ian Bell <ib...@pu...>* wrote: > > > From: Ian Bell <ib...@pu...> > Subject: Re: [Matplotlib-users] plotting points/locations from data file > To: "Michael Rawlins" <raw...@ya...> > Cc: Mat...@li... > Date: Tuesday, April 19, 2011, 7:22 PM > > > If you want to plot a given marker at the point, for instance a circle, > replace the last line of my code plt.text...... with > > plt.plot(lats,lons,'o') > > for a circle, or > > plt.plot(lats,lons,'s') > > for a square. Refer to Plot<http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot>for more information on the markers you can use. You are getting the error > because you have a delimiter different than a single space, so it isn't > splitting the line. Replace ' ' in the split command with your whitespace > delimiter. Is it a tab? Then you want '\t' . > > Good luck, > Ian > > ---- > Ian Bell > Graduate Research Assistant > Herrick Labs > Purdue University > email: ib...@pu... <http://mc/compose?to=ib...@pu...> > cell: (607)227-7626 > > > On Tue, Apr 19, 2011 at 7:14 PM, Michael Rawlins <raw...@ya...<http://mc/compose?to=raw...@ya...> > > wrote: > > > Yes, there is whitespace between each lat and lon on each line. But, > actually, I'd simply like to plot a dot at each location. The '1' was there > in my example because I do not yet know how to plot a particular symbol. > Here is what I got when I tried the code you just suggested. > > Traceback (most recent call last): > File "test.py", line 319, in <module> > > (lat,lon)=line.strip().split(' ') > ValueError: too many values to unpack > > > There are 203 records in the data file. Line 319 of test.py is this: > > > (lat,lon)=line.strip().split(' ') > > > --- On *Tue, 4/19/11, Ian Bell <ib...@pu...<http://mc/compose?to=ib...@pu...> > >* wrote: > > > From: Ian Bell <ib...@pu... <http://mc/compose?to=ib...@pu...>> > Subject: Re: [Matplotlib-users] plotting points/locations from data file > To: "Michael Rawlins" <raw...@ya...<http://mc/compose?to=raw...@ya...> > > > Cc: Mat...@li...<http://mc/compose?to=Mat...@li...> > Date: Tuesday, April 19, 2011, 6:52 PM > > > To clarify, you are trying to read in a set of (lat,lon) points in a file > that is space delimited, store the data, and then put a text marker at each > point, with each point numbered in order? The critical part is that you > want to use a list (or numpy array) instead of a dictionary. Something like > this ought to do (don't have MPL on this computer though - pretty sure this > should work): > > lines=open('file.txt','r').readlines() > (lats,lons)=([],[]) > for line in lines: > (lat,lon)=line.strip().split(' ') > lats.append(float(lat)) > lons.append(float(lon)) > > for i in range(len(lons)): > plt.text(lats[i],lon[i],str(i+1),ha='center',va='center',color='white') > > I'm sure there are a bunch of more compact ways to do this, but this should > work. > > Ian > ---- > Ian Bell > Graduate Research Assistant > Herrick Labs > Purdue University > email: ib...@pu... <http://mc/compose?to=ib...@pu...> > cell: (607)227-7626 > > > On Tue, Apr 19, 2011 at 4:09 PM, Michael Rawlins <raw...@ya...<http://mc/compose?to=raw...@ya...> > > wrote: > > > I'm trying to plot a series of points/locations on a map. I'm reading the > latitudes and longitudes from a file, with each lat, lon pair on each record > (line). Here is the code: > > def make_float(line): > lati, longi = line.split() > return float(lati), float(longi) > > my_dict = {} > with open("file.txt") as f: > for item in f: > lati,longi = make_float(item) > my_dict[lati] = longi > > xpt,ypt = m(-76.1670,39.4670 ) > plt.text(xpt,ypt,'1',color='white') > > #print my_dict > > The matplotlib code which I've previously used to plot a single point on > the map is below, with longitude and latitude in ( ): > > xpt,ypt = m(-70.758392,42.960445) > plt.text(xpt,ypt,'1',color='white') > > When replacing (-70.758392,42.960445) with (longi,lati), the code plots > only a single '1' at the location of just the last coordinate pair in the > file. So now I only need to plot them all. Does the code I've implemented > have an implicit loop to it? > > Mike > > > > > ------------------------------------------------------------------------------ > Benefiting from Server Virtualization: Beyond Initial Workload > Consolidation -- Increasing the use of server virtualization is a top > priority.Virtualization can reduce costs, simplify management, and improve > application availability and disaster protection. Learn more about boosting > the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev > _______________________________________________ > Matplotlib-users mailing list > Mat...@li...<http://mc/compose?to=Mat...@li...> > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > > > > > ------------------------------------------------------------------------------ > Benefiting from Server Virtualization: Beyond Initial Workload > Consolidation -- Increasing the use of server virtualization is a top > priority.Virtualization can reduce costs, simplify management, and improve > application availability and disaster protection. Learn more about boosting > the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > |
From: Ian B. <ib...@pu...> - 2011-04-19 23:35:32
|
Have to say I whole-heartedly agree with Glenn. One problem I have run into is a funky file headers where I want to skip lines 1,2,3, and 4, but line 3 is my real header line which doesn't work so well with either of the below solutions. I had to write my own wrapper to deal with these weird types of files. Ian ---- Ian Bell Graduate Research Assistant Herrick Labs Purdue University email: ib...@pu... cell: (607)227-7626 On Tue, Apr 19, 2011 at 7:32 PM, G Jones <gle...@gm...> wrote: > You may find it easier to use mlab.csv2rec or numpy.loadtxt. > > e.g. > > data = csv2rec(filename,delimiter=' ') > plot(data[:,0],data[:,1],'o') > > > > On Tue, Apr 19, 2011 at 4:26 PM, Michael Rawlins <raw...@ya...>wrote: > >> >> Sorry I should have mentioned that longitudes are negative; there is a '-' >> before each longitude, like so: >> >> 39.4670 -76.1670 >> 46.4000 -74.7670 >> 45.3830 -75.7170 >> 43.6170 -79.3830 >> 45.5170 -73.4170 >> >> >> Also the plt.text line you sent had lon[i] rather than lons[i]. I >> corrected that and changed my longitudes to not have the '-' sign and the >> code ran without error. Could the '-' be causing a problem? I need to input >> the lat, lon as in the file as shown above. >> >> Mike >> >> >> --- On *Tue, 4/19/11, Ian Bell <ib...@pu...>* wrote: >> >> >> From: Ian Bell <ib...@pu...> >> Subject: Re: [Matplotlib-users] plotting points/locations from data file >> To: "Michael Rawlins" <raw...@ya...> >> Cc: Mat...@li... >> Date: Tuesday, April 19, 2011, 7:22 PM >> >> >> If you want to plot a given marker at the point, for instance a circle, >> replace the last line of my code plt.text...... with >> >> plt.plot(lats,lons,'o') >> >> for a circle, or >> >> plt.plot(lats,lons,'s') >> >> for a square. Refer to Plot<http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot>for more information on the markers you can use. You are getting the error >> because you have a delimiter different than a single space, so it isn't >> splitting the line. Replace ' ' in the split command with your whitespace >> delimiter. Is it a tab? Then you want '\t' . >> >> Good luck, >> Ian >> >> ---- >> Ian Bell >> Graduate Research Assistant >> Herrick Labs >> Purdue University >> email: ib...@pu... <http://mc/compose?to=ib...@pu...> >> cell: (607)227-7626 >> >> >> On Tue, Apr 19, 2011 at 7:14 PM, Michael Rawlins <raw...@ya...<http://mc/compose?to=raw...@ya...> >> > wrote: >> >> >> Yes, there is whitespace between each lat and lon on each line. But, >> actually, I'd simply like to plot a dot at each location. The '1' was there >> in my example because I do not yet know how to plot a particular symbol. >> Here is what I got when I tried the code you just suggested. >> >> Traceback (most recent call last): >> File "test.py", line 319, in <module> >> >> (lat,lon)=line.strip().split(' ') >> ValueError: too many values to unpack >> >> >> There are 203 records in the data file. Line 319 of test.py is this: >> >> >> (lat,lon)=line.strip().split(' ') >> >> >> --- On *Tue, 4/19/11, Ian Bell <ib...@pu...<http://mc/compose?to=ib...@pu...> >> >* wrote: >> >> >> From: Ian Bell <ib...@pu... <http://mc/compose?to=ib...@pu...>> >> Subject: Re: [Matplotlib-users] plotting points/locations from data file >> To: "Michael Rawlins" <raw...@ya...<http://mc/compose?to=raw...@ya...> >> > >> Cc: Mat...@li...<http://mc/compose?to=Mat...@li...> >> Date: Tuesday, April 19, 2011, 6:52 PM >> >> >> To clarify, you are trying to read in a set of (lat,lon) points in a file >> that is space delimited, store the data, and then put a text marker at each >> point, with each point numbered in order? The critical part is that you >> want to use a list (or numpy array) instead of a dictionary. Something like >> this ought to do (don't have MPL on this computer though - pretty sure this >> should work): >> >> lines=open('file.txt','r').readlines() >> (lats,lons)=([],[]) >> for line in lines: >> (lat,lon)=line.strip().split(' ') >> lats.append(float(lat)) >> lons.append(float(lon)) >> >> for i in range(len(lons)): >> >> plt.text(lats[i],lon[i],str(i+1),ha='center',va='center',color='white') >> >> I'm sure there are a bunch of more compact ways to do this, but this >> should work. >> >> Ian >> ---- >> Ian Bell >> Graduate Research Assistant >> Herrick Labs >> Purdue University >> email: ib...@pu... <http://mc/compose?to=ib...@pu...> >> cell: (607)227-7626 >> >> >> On Tue, Apr 19, 2011 at 4:09 PM, Michael Rawlins <raw...@ya...<http://mc/compose?to=raw...@ya...> >> > wrote: >> >> >> I'm trying to plot a series of points/locations on a map. I'm reading the >> latitudes and longitudes from a file, with each lat, lon pair on each record >> (line). Here is the code: >> >> def make_float(line): >> lati, longi = line.split() >> return float(lati), float(longi) >> >> my_dict = {} >> with open("file.txt") as f: >> for item in f: >> lati,longi = make_float(item) >> my_dict[lati] = longi >> >> xpt,ypt = m(-76.1670,39.4670 ) >> plt.text(xpt,ypt,'1',color='white') >> >> #print my_dict >> >> The matplotlib code which I've previously used to plot a single point on >> the map is below, with longitude and latitude in ( ): >> >> xpt,ypt = m(-70.758392,42.960445) >> plt.text(xpt,ypt,'1',color='white') >> >> When replacing (-70.758392,42.960445) with (longi,lati), the code plots >> only a single '1' at the location of just the last coordinate pair in the >> file. So now I only need to plot them all. Does the code I've implemented >> have an implicit loop to it? >> >> Mike >> >> >> >> >> ------------------------------------------------------------------------------ >> Benefiting from Server Virtualization: Beyond Initial Workload >> Consolidation -- Increasing the use of server virtualization is a top >> priority.Virtualization can reduce costs, simplify management, and improve >> application availability and disaster protection. Learn more about >> boosting >> the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev >> _______________________________________________ >> Matplotlib-users mailing list >> Mat...@li...<http://mc/compose?to=Mat...@li...> >> https://lists.sourceforge.net/lists/listinfo/matplotlib-users >> >> >> >> >> >> ------------------------------------------------------------------------------ >> Benefiting from Server Virtualization: Beyond Initial Workload >> Consolidation -- Increasing the use of server virtualization is a top >> priority.Virtualization can reduce costs, simplify management, and improve >> application availability and disaster protection. Learn more about >> boosting >> the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev >> _______________________________________________ >> Matplotlib-users mailing list >> Mat...@li... >> https://lists.sourceforge.net/lists/listinfo/matplotlib-users >> >> > |
From: Michael R. <raw...@ya...> - 2011-04-20 00:03:15
|
Do I need to add something to the header(?) at top of file to use csv2rec? import sys,getopt from mpl_toolkits.basemap import Basemap, shiftgrid, cm from mpl_toolkits.basemap import NetCDFFile from pylab import * --- On Tue, 4/19/11, Ian Bell <ib...@pu...> wrote: From: Ian Bell <ib...@pu...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "G Jones" <gle...@gm...> Cc: "Michael Rawlins" <raw...@ya...>, Mat...@li... Date: Tuesday, April 19, 2011, 7:35 PM Have to say I whole-heartedly agree with Glenn. One problem I have run into is a funky file headers where I want to skip lines 1,2,3, and 4, but line 3 is my real header line which doesn't work so well with either of the below solutions. I had to write my own wrapper to deal with these weird types of files. Ian ---- Ian Bell Graduate Research Assistant Herrick Labs Purdue University email: ib...@pu... cell: (607)227-7626 On Tue, Apr 19, 2011 at 7:32 PM, G Jones <gle...@gm...> wrote: You may find it easier to use mlab.csv2rec or numpy.loadtxt. e.g. data = csv2rec(filename,delimiter=' ') plot(data[:,0],data[:,1],'o') On Tue, Apr 19, 2011 at 4:26 PM, Michael Rawlins <raw...@ya...> wrote: Sorry I should have mentioned that longitudes are negative; there is a '-' before each longitude, like so: 39.4670 -76.1670 46.4000 -74.7670 45.3830 -75.7170 43.6170 -79.3830 45.5170 -73.4170 Also the plt.text line you sent had lon[i] rather than lons[i]. I corrected that and changed my longitudes to not have the '-' sign and the code ran without error. Could the '-' be causing a problem? I need to input the lat, lon as in the file as shown above. Mike --- On Tue, 4/19/11, Ian Bell <ib...@pu...> wrote: From: Ian Bell <ib...@pu...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "Michael Rawlins" <raw...@ya...> Cc: Mat...@li... Date: Tuesday, April 19, 2011, 7:22 PM If you want to plot a given marker at the point, for instance a circle, replace the last line of my code plt.text...... with plt.plot(lats,lons,'o') for a circle, or plt.plot(lats,lons,'s') for a square. Refer to Plot for more information on the markers you can use. You are getting the error because you have a delimiter different than a single space, so it isn't splitting the line. Replace ' ' in the split command with your whitespace delimiter. Is it a tab? Then you want '\t' . Good luck, Ian ---- Ian Bell Graduate Research Assistant Herrick Labs Purdue University email: ib...@pu... cell: (607)227-7626 On Tue, Apr 19, 2011 at 7:14 PM, Michael Rawlins <raw...@ya...> wrote: Yes, there is whitespace between each lat and lon on each line. But, actually, I'd simply like to plot a dot at each location. The '1' was there in my example because I do not yet know how to plot a particular symbol. Here is what I got when I tried the code you just suggested. Traceback (most recent call last): File "test.py", line 319, in <module> (lat,lon)=line.strip().split(' ') ValueError: too many values to unpack There are 203 records in the data file. Line 319 of test.py is this: (lat,lon)=line.strip().split(' ') --- On Tue, 4/19/11, Ian Bell <ib...@pu...> wrote: From: Ian Bell <ib...@pu...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "Michael Rawlins" <raw...@ya...> Cc: Mat...@li... Date: Tuesday, April 19, 2011, 6:52 PM To clarify, you are trying to read in a set of (lat,lon) points in a file that is space delimited, store the data, and then put a text marker at each point, with each point numbered in order? The critical part is that you want to use a list (or numpy array) instead of a dictionary. Something like this ought to do (don't have MPL on this computer though - pretty sure this should work): lines=open('file.txt','r').readlines() (lats,lons)=([],[]) for line in lines: (lat,lon)=line.strip().split(' ') lats.append(float(lat)) lons.append(float(lon)) for i in range(len(lons)): plt.text(lats[i],lon[i],str(i+1),ha='center',va='center',color='white') I'm sure there are a bunch of more compact ways to do this, but this should work. Ian ---- Ian Bell Graduate Research Assistant Herrick Labs Purdue University email: ib...@pu... cell: (607)227-7626 On Tue, Apr 19, 2011 at 4:09 PM, Michael Rawlins <raw...@ya...> wrote: I'm trying to plot a series of points/locations on a map. I'm reading the latitudes and longitudes from a file, with each lat, lon pair on each record (line). Here is the code: def make_float(line): lati, longi = line.split() return float(lati), float(longi) my_dict = {} with open("file.txt") as f: for item in f: lati,longi = make_float(item) my_dict[lati] = longi xpt,ypt = m(-76.1670,39.4670 ) plt.text(xpt,ypt,'1',color='white') #print my_dict The matplotlib code which I've previously used to plot a single point on the map is below, with longitude and latitude in ( ): xpt,ypt = m(-70.758392,42.960445) plt.text(xpt,ypt,'1',color='white') When replacing (-70.758392,42.960445) with (longi,lati), the code plots only a single '1' at the location of just the last coordinate pair in the file. So now I only need to plot them all. Does the code I've implemented have an implicit loop to it? Mike ------------------------------------------------------------------------------ Benefiting from Server Virtualization: Beyond Initial Workload Consolidation -- Increasing the use of server virtualization is a top priority.Virtualization can reduce costs, simplify management, and improve application availability and disaster protection. Learn more about boosting the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev _______________________________________________ Matplotlib-users mailing list Mat...@li... https://lists.sourceforge.net/lists/listinfo/matplotlib-users ------------------------------------------------------------------------------ Benefiting from Server Virtualization: Beyond Initial Workload Consolidation -- Increasing the use of server virtualization is a top priority.Virtualization can reduce costs, simplify management, and improve application availability and disaster protection. Learn more about boosting the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev _______________________________________________ Matplotlib-users mailing list Mat...@li... https://lists.sourceforge.net/lists/listinfo/matplotlib-users |
From: G J. <gle...@gm...> - 2011-04-20 00:12:30
|
No need for a header, but I guess my example was a little too simple. You could do: data = csv2rec(filename,delimiter=' ',names=['lat','lon']) plot(data['lat'],data['lon'],'o') or you could do data = np.loadtxt(filename) plot(data[:,0],data[:,1],'o') In general, I strongly recommend developing with ipython --pylab. That way all the documentation is at your fingertips using the ? and ?? notation. On Tue, Apr 19, 2011 at 5:03 PM, Michael Rawlins <raw...@ya...>wrote: > > Do I need to add something to the header(?) at top of file to use csv2rec? > > > import sys,getopt > > from mpl_toolkits.basemap import Basemap, shiftgrid, cm > from mpl_toolkits.basemap import NetCDFFile > from pylab import * > > > > > --- On *Tue, 4/19/11, Ian Bell <ib...@pu...>* wrote: > > > From: Ian Bell <ib...@pu...> > Subject: Re: [Matplotlib-users] plotting points/locations from data file > To: "G Jones" <gle...@gm...> > Cc: "Michael Rawlins" <raw...@ya...>, > Mat...@li... > Date: Tuesday, April 19, 2011, 7:35 PM > > > Have to say I whole-heartedly agree with Glenn. One problem I have run > into is a funky file headers where I want to skip lines 1,2,3, and 4, but > line 3 is my real header line which doesn't work so well with either of the > below solutions. I had to write my own wrapper to deal with these weird > types of files. > > Ian > > ---- > Ian Bell > Graduate Research Assistant > Herrick Labs > Purdue University > email: ib...@pu... <http://mc/compose?to=ib...@pu...> > cell: (607)227-7626 > > > On Tue, Apr 19, 2011 at 7:32 PM, G Jones <gle...@gm...<http://mc/compose?to=gle...@gm...> > > wrote: > > You may find it easier to use mlab.csv2rec or numpy.loadtxt. > > e.g. > > data = csv2rec(filename,delimiter=' ') > plot(data[:,0],data[:,1],'o') > > > > On Tue, Apr 19, 2011 at 4:26 PM, Michael Rawlins <raw...@ya...<http://mc/compose?to=raw...@ya...> > > wrote: > > > Sorry I should have mentioned that longitudes are negative; there is a '-' > before each longitude, like so: > > 39.4670 -76.1670 > 46.4000 -74.7670 > 45.3830 -75.7170 > 43.6170 -79.3830 > 45.5170 -73.4170 > > > Also the plt.text line you sent had lon[i] rather than lons[i]. I > corrected that and changed my longitudes to not have the '-' sign and the > code ran without error. Could the '-' be causing a problem? I need to input > the lat, lon as in the file as shown above. > > Mike > > > --- On *Tue, 4/19/11, Ian Bell <ib...@pu...<http://mc/compose?to=ib...@pu...> > >* wrote: > > > From: Ian Bell <ib...@pu... <http://mc/compose?to=ib...@pu...>> > Subject: Re: [Matplotlib-users] plotting points/locations from data file > To: "Michael Rawlins" <raw...@ya...<http://mc/compose?to=raw...@ya...> > > > Cc: Mat...@li...<http://mc/compose?to=Mat...@li...> > Date: Tuesday, April 19, 2011, 7:22 PM > > > If you want to plot a given marker at the point, for instance a circle, > replace the last line of my code plt.text...... with > > plt.plot(lats,lons,'o') > > for a circle, or > > plt.plot(lats,lons,'s') > > for a square. Refer to Plot<http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot>for more information on the markers you can use. You are getting the error > because you have a delimiter different than a single space, so it isn't > splitting the line. Replace ' ' in the split command with your whitespace > delimiter. Is it a tab? Then you want '\t' . > > Good luck, > Ian > > ---- > Ian Bell > Graduate Research Assistant > Herrick Labs > Purdue University > email: ib...@pu... <http://mc/compose?to=ib...@pu...> > cell: (607)227-7626 > > > On Tue, Apr 19, 2011 at 7:14 PM, Michael Rawlins <raw...@ya...<http://mc/compose?to=raw...@ya...> > > wrote: > > > Yes, there is whitespace between each lat and lon on each line. But, > actually, I'd simply like to plot a dot at each location. The '1' was there > in my example because I do not yet know how to plot a particular symbol. > Here is what I got when I tried the code you just suggested. > > Traceback (most recent call last): > File "test.py", line 319, in <module> > > (lat,lon)=line.strip().split(' ') > ValueError: too many values to unpack > > > There are 203 records in the data file. Line 319 of test.py is this: > > > (lat,lon)=line.strip().split(' ') > > > --- On *Tue, 4/19/11, Ian Bell <ib...@pu...<http://mc/compose?to=ib...@pu...> > >* wrote: > > > From: Ian Bell <ib...@pu... <http://mc/compose?to=ib...@pu...>> > Subject: Re: [Matplotlib-users] plotting points/locations from data file > To: "Michael Rawlins" <raw...@ya...<http://mc/compose?to=raw...@ya...> > > > Cc: Mat...@li...<http://mc/compose?to=Mat...@li...> > Date: Tuesday, April 19, 2011, 6:52 PM > > > To clarify, you are trying to read in a set of (lat,lon) points in a file > that is space delimited, store the data, and then put a text marker at each > point, with each point numbered in order? The critical part is that you > want to use a list (or numpy array) instead of a dictionary. Something like > this ought to do (don't have MPL on this computer though - pretty sure this > should work): > > lines=open('file.txt','r').readlines() > (lats,lons)=([],[]) > for line in lines: > (lat,lon)=line.strip().split(' ') > lats.append(float(lat)) > lons.append(float(lon)) > > for i in range(len(lons)): > plt.text(lats[i],lon[i],str(i+1),ha='center',va='center',color='white') > > I'm sure there are a bunch of more compact ways to do this, but this should > work. > > Ian > ---- > Ian Bell > Graduate Research Assistant > Herrick Labs > Purdue University > email: ib...@pu... <http://mc/compose?to=ib...@pu...> > cell: (607)227-7626 > > > On Tue, Apr 19, 2011 at 4:09 PM, Michael Rawlins <raw...@ya...<http://mc/compose?to=raw...@ya...> > > wrote: > > > I'm trying to plot a series of points/locations on a map. I'm reading the > latitudes and longitudes from a file, with each lat, lon pair on each record > (line). Here is the code: > > def make_float(line): > lati, longi = line.split() > return float(lati), float(longi) > > my_dict = {} > with open("file.txt") as f: > for item in f: > lati,longi = make_float(item) > my_dict[lati] = longi > > xpt,ypt = m(-76.1670,39.4670 ) > plt.text(xpt,ypt,'1',color='white') > > #print my_dict > > The matplotlib code which I've previously used to plot a single point on > the map is below, with longitude and latitude in ( ): > > xpt,ypt = m(-70.758392,42.960445) > plt.text(xpt,ypt,'1',color='white') > > When replacing (-70.758392,42.960445) with (longi,lati), the code plots > only a single '1' at the location of just the last coordinate pair in the > file. So now I only need to plot them all. Does the code I've implemented > have an implicit loop to it? > > Mike > > > > > ------------------------------------------------------------------------------ > Benefiting from Server Virtualization: Beyond Initial Workload > Consolidation -- Increasing the use of server virtualization is a top > priority.Virtualization can reduce costs, simplify management, and improve > application availability and disaster protection. Learn more about boosting > the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev > _______________________________________________ > Matplotlib-users mailing list > Mat...@li...<http://mc/compose?to=Mat...@li...> > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > > > > > ------------------------------------------------------------------------------ > Benefiting from Server Virtualization: Beyond Initial Workload > Consolidation -- Increasing the use of server virtualization is a top > priority.Virtualization can reduce costs, simplify management, and improve > application availability and disaster protection. Learn more about boosting > the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev > _______________________________________________ > Matplotlib-users mailing list > Mat...@li...<http://mc/compose?to=Mat...@li...> > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > > > |
From: Michael R. <raw...@ya...> - 2011-04-20 00:26:32
|
The first example produced no plotted symbols but no errors on execution. The second example produced this: Plotting, please wait...maybe more than 10 seconds Traceback (most recent call last): File "testNew.py", line 137, in <module> data = np.loadtxt('file2.txt') File "/usr/lib/python2.6/dist-packages/numpy/lib/io.py", line 489, in loadtxt X.append(tuple([conv(val) for (conv, val) in zip(converters, vals)])) ValueError: invalid literal for float(): 39.4670, Grrrr...... My code follows. I believe this is standard python and matplotlib. Should produce a map of Northeast US. Perhaps someone could get this working with a few example points: 39.4670, -76.1670 46.4000, -74.7670 45.3830, -75.7170 43.6170, -79.3830 45.5170, -73.4170 45.6170, -74.4170 43.8330, -77.1500 43.9500, -78.1670 43.2500, -79.2170 43.8330, -66.0830 #!/usr/bin/env python # v0.5 19 June 2010 # General purpose plotter of 2-D gridded data from NetCDF files, # plotted with map boundaries. # NetCDF file should have data in either 2-D, 3-D or 4-D arrays. # Works with the netCDF files in the tutorial, and also the # files available for download at: # http://www.cdc.noaa.gov/cdc/data.ncep.reanalysis.html # Adapted from the basemap example plotmap_pcolor.py, # Some of the variable names from that example are retained. # # Uses basemap's pcolor function. Pcolor accepts arrays # of the longitude and latitude points of the vertices on the pixels, # as well as an array with the numerical value in the pixel. verbose=0 #verbose=2 says a bit more import sys,getopt from mpl_toolkits.basemap import Basemap, shiftgrid, cm #from netCDF3 import Dataset as NetCDFFile from mpl_toolkits.basemap import NetCDFFile from pylab import * #from matplotlib.mlab import csv2rec alloptions, otherargs= getopt.getopt(sys.argv[1:],'ro:p:X:Y:v:t:l:u:n:') # note the : after o and p proj='lam' #plotfile=None #plotfile='testmap2.png' usejetrev=False colorbounds=[None,None] extratext="" xvar=None yvar=None thevar=None therec=None thelev=None cbot=None ctop=None startlon=-180 #default assumption for starting longitude for theopt,thearg in alloptions: print theopt,thearg if theopt=='-o': # -o needs filename after it, which is now thearg plotfile=thearg elif theopt=='-p': proj=thearg elif theopt=='-X': xvar=thearg elif theopt=='-Y': yvar=thearg elif theopt=='-v': thevar=thearg elif theopt=='-t': thetitle=thearg elif theopt=='-l': cbot=thearg elif theopt=='-u': ctop=thearg elif theopt=='-n': therec=thearg elif theopt=='-m': thelev=thearg elif theopt=='-r': usejetrev=True else: #something went wrong print "hmm, what are these??? ", theopt, thearg sys.exit() print "\nPlotting, please wait...maybe more than 10 seconds" if proj=='lam': #Lambert Conformal m = Basemap(llcrnrlon=-80.6,llcrnrlat=38.4,urcrnrlon=-66.0,urcrnrlat=47.7,\ resolution='l',area_thresh=1000.,projection='lcc',\ lat_1=65.,lon_0=-73.3) xtxt=200000. #offset for text ytxt=200000. parallels = arange(38.,48.,4.) meridians = arange(-80.,-68.,4.) else: #cylindrical is default # m = Basemap(llcrnrlon=-180.,llcrnrlat=-90,urcrnrlon=180.,urcrnrlat=90.,\ # resolution='c',area_thresh=10000.,projection='cyl') m = Basemap(llcrnrlon=startlon,llcrnrlat=-90,urcrnrlon=startlon+360.,urcrnrlat=90.,\ resolution='c',area_thresh=10000.,projection='cyl') xtxt=1. ytxt=0. parallels = arange(-90.,90.,30.) if startlon==-180: meridians = arange(-180.,180.,60.) else: meridians = arange(0.,360.,60.) if verbose>1: print m.__doc__ xsize = rcParams['figure.figsize'][0] fig=figure(figsize=(xsize,m.aspect*xsize)) #ax = fig.add_axes([0.08,0.1,0.7,0.7],axisbg='white') ax = fig.add_axes([0.06,0.00,0.8,1.0],axisbg='white') # make a pcolor plot. #x, y = m(lons, lats) #p = m.pcolor(x,y,maskdat,shading='flat',cmap=cmap) #clim(*colorbounds) # axes units units are left, bottom, width, height #cax = axes([0.85, 0.1, 0.05, 0.7]) # colorbar axes for map w/ no graticule cax = axes([0.88, 0.1, 0.06, 0.81]) # colorbar axes for map w/ graticule axes(ax) # make the original axes current again ######### Plot symbol at station locations ################# #lines=open('file2.txt','r').readlines() #(lats,lons)=([],[]) #for line in lines: # (lat,lon)=line.strip().split(',') # lats.append(float(lat)) # lons.append(float(lon)) #for i in range(len(lons)): # plt.plot(lats,lons,'*') #plt.plot(lons,lats,'*') #data = csv2rec('file2.txt',delimiter=',') #plot(data[:,0],data[:,1],'o') #data = csv2rec('file2.txt',delimiter=' ',names=['lat','lon']) #plot(data['lat'],data['lon'],'o') data = np.loadtxt('file2.txt') plot(data[:,0],data[:,1],'o') xpt,ypt = m(-75.0,43.0) text(xpt,ypt,'*') # draw coastlines and political boundaries. m.drawcoastlines() m.drawcountries() m.drawstates() # draw parallels and meridians. # label on left, right and bottom of map. m.drawparallels(parallels,labels=[1,0,0,0]) m.drawmeridians(meridians,labels=[1,1,0,1]) #if plotfile: # savefig(plotfile, dpi=72, facecolor='w', bbox_inches='tight', edgecolor='w', orientation='portrait') #else: # show() #plt.savefig('map.png') plt.savefig('map.eps') # comment show to mass produce --- On Tue, 4/19/11, G Jones <gle...@gm...> wrote: From: G Jones <gle...@gm...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "Michael Rawlins" <raw...@ya...> Cc: "Ian Bell" <ib...@pu...>, Mat...@li... Date: Tuesday, April 19, 2011, 8:12 PM No need for a header, but I guess my example was a little too simple. You could do: data = csv2rec(filename,delimiter=' ',names=['lat','lon']) plot(data['lat'],data['lon'],'o') or you could do data = np.loadtxt(filename) plot(data[:,0],data[:,1],'o') In general, I strongly recommend developing with ipython --pylab. That way all the documentation is at your fingertips using the ? and ?? notation. |
From: Michael R. <raw...@ya...> - 2011-04-20 00:30:46
|
On second thought, the code requires basemap package too. Mike --- On Tue, 4/19/11, Michael Rawlins <raw...@ya...> wrote: From: Michael Rawlins <raw...@ya...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "G Jones" <gle...@gm...> Cc: Mat...@li... Date: Tuesday, April 19, 2011, 8:26 PM The first example produced no plotted symbols but no errors on execution. The second example produced this: Plotting, please wait...maybe more than 10 seconds Traceback (most recent call last): File "testNew.py", line 137, in <module> data = np.loadtxt('file2.txt') File "/usr/lib/python2.6/dist-packages/numpy/lib/io.py", line 489, in loadtxt X.append(tuple([conv(val) for (conv, val) in zip(converters, vals)])) ValueError: invalid literal for float(): 39.4670, Grrrr...... My code follows. I believe this is standard python and matplotlib. Should produce a map of Northeast US. Perhaps someone could get this working with a few example points: 39.4670, -76.1670 46.4000, -74.7670 45.3830, -75.7170 43.6170, -79.3830 45.5170, -73.4170 45.6170, -74.4170 43.8330, -77.1500 43.9500, -78.1670 43.2500, -79.2170 43.8330, -66.0830 #!/usr/bin/env python # v0.5 19 June 2010 # General purpose plotter of 2-D gridded data from NetCDF files, # plotted with map boundaries. # NetCDF file should have data in either 2-D, 3-D or 4-D arrays. # Works with the netCDF files in the tutorial, and also the # files available for download at: # http://www.cdc.noaa.gov/cdc/data.ncep.reanalysis.html # Adapted from the basemap example plotmap_pcolor.py, # Some of the variable names from that example are retained. # # Uses basemap's pcolor function. Pcolor accepts arrays # of the longitude and latitude points of the vertices on the pixels, # as well as an array with the numerical value in the pixel. verbose=0 #verbose=2 says a bit more import sys,getopt from mpl_toolkits.basemap import Basemap, shiftgrid, cm #from netCDF3 import Dataset as NetCDFFile from mpl_toolkits.basemap import NetCDFFile from pylab import * #from matplotlib.mlab import csv2rec alloptions, otherargs= getopt.getopt(sys.argv[1:],'ro:p:X:Y:v:t:l:u:n:') # note the : after o and p proj='lam' #plotfile=None #plotfile='testmap2.png' usejetrev=False colorbounds=[None,None] extratext="" xvar=None yvar=None thevar=None therec=None thelev=None cbot=None ctop=None startlon=-180 #default assumption for starting longitude for theopt,thearg in alloptions: print theopt,thearg if theopt=='-o': # -o needs filename after it, which is now thearg plotfile=thearg elif theopt=='-p': proj=thearg elif theopt=='-X': xvar=thearg elif theopt=='-Y': yvar=thearg elif theopt=='-v': thevar=thearg elif theopt=='-t': thetitle=thearg elif theopt=='-l': cbot=thearg elif theopt=='-u': ctop=thearg elif theopt=='-n': therec=thearg elif theopt=='-m': thelev=thearg elif theopt=='-r': usejetrev=True else: #something went wrong print "hmm, what are these??? ", theopt, thearg sys.exit() print "\nPlotting, please wait...maybe more than 10 seconds" if proj=='lam': #Lambert Conformal m = Basemap(llcrnrlon=-80.6,llcrnrlat=38.4,urcrnrlon=-66.0,urcrnrlat=47.7,\ resolution='l',area_thresh=1000.,projection='lcc',\ lat_1=65.,lon_0=-73.3) xtxt=200000. #offset for text ytxt=200000. parallels = arange(38.,48.,4.) meridians = arange(-80.,-68.,4.) else: #cylindrical is default # m = Basemap(llcrnrlon=-180.,llcrnrlat=-90,urcrnrlon=180.,urcrnrlat=90.,\ # resolution='c',area_thresh=10000.,projection='cyl') m = Basemap(llcrnrlon=startlon,llcrnrlat=-90,urcrnrlon=startlon+360.,urcrnrlat=90.,\ resolution='c',area_thresh=10000.,projection='cyl') xtxt=1. ytxt=0. parallels = arange(-90.,90.,30.) if startlon==-180: meridians = arange(-180.,180.,60.) else: meridians = arange(0.,360.,60.) if verbose>1: print m.__doc__ xsize = rcParams['figure.figsize'][0] fig=figure(figsize=(xsize,m.aspect*xsize)) #ax = fig.add_axes([0.08,0.1,0.7,0.7],axisbg='white') ax = fig.add_axes([0.06,0.00,0.8,1.0],axisbg='white') # make a pcolor plot. #x, y = m(lons, lats) #p = m.pcolor(x,y,maskdat,shading='flat',cmap=cmap) #clim(*colorbounds) # axes units units are left, bottom, width, height #cax = axes([0.85, 0.1, 0.05, 0.7]) # colorbar axes for map w/ no graticule cax = axes([0.88, 0.1, 0.06, 0.81]) # colorbar axes for map w/ graticule axes(ax) # make the original axes current again ######### Plot symbol at station locations ################# #lines=open('file2.txt','r').readlines() #(lats,lons)=([],[]) #for line in lines: # (lat,lon)=line.strip().split(',') # lats.append(float(lat)) # lons.append(float(lon)) #for i in range(len(lons)): # plt.plot(lats,lons,'*') #plt.plot(lons,lats,'*') #data = csv2rec('file2.txt',delimiter=',') #plot(data[:,0],data[:,1],'o') #data = csv2rec('file2.txt',delimiter=' ',names=['lat','lon']) #plot(data['lat'],data['lon'],'o') data = np.loadtxt('file2.txt') plot(data[:,0],data[:,1],'o') xpt,ypt = m(-75.0,43.0) text(xpt,ypt,'*') # draw coastlines and political boundaries. m.drawcoastlines() m.drawcountries() m.drawstates() # draw parallels and meridians. # label on left, right and bottom of map. m.drawparallels(parallels,labels=[1,0,0,0]) m.drawmeridians(meridians,labels=[1,1,0,1]) #if plotfile: # savefig(plotfile, dpi=72, facecolor='w', bbox_inches='tight', edgecolor='w', orientation='portrait') #else: # show() #plt.savefig('map.png') plt.savefig('map.eps') # comment show to mass produce --- On Tue, 4/19/11, G Jones <gle...@gm...> wrote: From: G Jones <gle...@gm...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "Michael Rawlins" <raw...@ya...> Cc: "Ian Bell" <ib...@pu...>, Mat...@li... Date: Tuesday, April 19, 2011, 8:12 PM No need for a header, but I guess my example was a little too simple. You could do: data = csv2rec(filename,delimiter=' ',names=['lat','lon']) plot(data['lat'],data['lon'],'o') or you could do data = np.loadtxt(filename) plot(data[:,0],data[:,1],'o') In general, I strongly recommend developing with ipython --pylab. That way all the documentation is at your fingertips using the ? and ?? notation. -----Inline Attachment Follows----- ------------------------------------------------------------------------------ Benefiting from Server Virtualization: Beyond Initial Workload Consolidation -- Increasing the use of server virtualization is a top priority.Virtualization can reduce costs, simplify management, and improve application availability and disaster protection. Learn more about boosting the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev -----Inline Attachment Follows----- _______________________________________________ Matplotlib-users mailing list Mat...@li... https://lists.sourceforge.net/lists/listinfo/matplotlib-users |
From: G J. <gle...@gm...> - 2011-04-20 00:31:13
|
As you can see from the error message, it's trying to convert "39.4670," to a float and complaining that this is not a valid value (because of the comma. The examples I suggested were for your original space delimited data. For comma delimited data you'll want to remove the delimiter argument to csv2rec (or explicitly set delimiter=',' if you prefer). If you want to use loadtxt, you can set delimiter=',' Again, read the doc strings to see what these functions are expecting by default. On Tue, Apr 19, 2011 at 5:26 PM, Michael Rawlins <raw...@ya...>wrote: > > The first example produced no plotted symbols but no errors on execution. > The second example produced this: > > Plotting, please wait...maybe more than 10 seconds > > Traceback (most recent call last): > File "testNew.py", line 137, in <module> > data = np.loadtxt('file2.txt') > File "/usr/lib/python2.6/dist-packages/numpy/lib/io.py", line 489, in > loadtxt > X.append(tuple([conv(val) for (conv, val) in zip(converters, vals)])) > ValueError: invalid literal for float(): 39.4670, > > > Grrrr...... > > My code follows. I believe this is standard python and matplotlib. Should > produce a map of Northeast US. Perhaps someone could get this working with > a few example points: > > > 39.4670, -76.1670 > 46.4000, -74.7670 > 45.3830, -75.7170 > 43.6170, -79.3830 > 45.5170, -73.4170 > 45.6170, -74.4170 > 43.8330, -77.1500 > 43.9500, -78.1670 > 43.2500, -79.2170 > 43.8330, -66.0830 > > > #!/usr/bin/env python > # v0.5 19 June 2010 > # General purpose plotter of 2-D gridded data from NetCDF files, > # plotted with map boundaries. > # NetCDF file should have data in either 2-D, 3-D or 4-D arrays. > # Works with the netCDF files in the tutorial, and also the > # files available for download at: > # http://www.cdc.noaa.gov/cdc/data.ncep.reanalysis.html > # Adapted from the basemap example plotmap_pcolor.py, > # Some of the variable names from that example are retained. > # > # Uses basemap's pcolor function. Pcolor accepts arrays > # of the longitude and latitude points of the vertices on the pixels, > # as well as an array with the numerical value in the pixel. > > verbose=0 #verbose=2 says a bit more > > > import sys,getopt > > from mpl_toolkits.basemap import Basemap, shiftgrid, cm > #from netCDF3 import Dataset as NetCDFFile > from mpl_toolkits.basemap import NetCDFFile > from pylab import * > #from matplotlib.mlab import csv2rec > > alloptions, otherargs= getopt.getopt(sys.argv[1:],'ro:p:X:Y:v:t:l:u:n:') # > note the : after o and p > proj='lam' > #plotfile=None > #plotfile='testmap2.png' > usejetrev=False > colorbounds=[None,None] > extratext="" > xvar=None > yvar=None > thevar=None > > therec=None > thelev=None > cbot=None > ctop=None > startlon=-180 #default assumption for starting longitude > for theopt,thearg in alloptions: > print theopt,thearg > if theopt=='-o': # -o needs filename after it, which is now thearg > plotfile=thearg > elif theopt=='-p': > proj=thearg > elif theopt=='-X': > xvar=thearg > elif theopt=='-Y': > yvar=thearg > elif theopt=='-v': > thevar=thearg > elif theopt=='-t': > thetitle=thearg > elif theopt=='-l': > cbot=thearg > elif theopt=='-u': > ctop=thearg > elif theopt=='-n': > therec=thearg > elif theopt=='-m': > thelev=thearg > elif theopt=='-r': > usejetrev=True > else: #something went wrong > print "hmm, what are these??? ", theopt, thearg > sys.exit() > > print "\nPlotting, please wait...maybe more than 10 seconds" > if proj=='lam': #Lambert Conformal > m = > Basemap(llcrnrlon=-80.6,llcrnrlat=38.4,urcrnrlon=-66.0,urcrnrlat=47.7,\ > resolution='l',area_thresh=1000.,projection='lcc',\ > lat_1=65.,lon_0=-73.3) > xtxt=200000. #offset for text > ytxt=200000. > parallels = arange(38.,48.,4.) > meridians = arange(-80.,-68.,4.) > else: #cylindrical is default > # m = > Basemap(llcrnrlon=-180.,llcrnrlat=-90,urcrnrlon=180.,urcrnrlat=90.,\ > # resolution='c',area_thresh=10000.,projection='cyl') > m = > Basemap(llcrnrlon=startlon,llcrnrlat=-90,urcrnrlon=startlon+360.,urcrnrlat=90.,\ > resolution='c',area_thresh=10000.,projection='cyl') > xtxt=1. > ytxt=0. > parallels = arange(-90.,90.,30.) > if startlon==-180: > meridians = arange(-180.,180.,60.) > else: > meridians = arange(0.,360.,60.) > > if verbose>1: print m.__doc__ > xsize = rcParams['figure.figsize'][0] > fig=figure(figsize=(xsize,m.aspect*xsize)) > #ax = fig.add_axes([0.08,0.1,0.7,0.7],axisbg='white') > ax = fig.add_axes([0.06,0.00,0.8,1.0],axisbg='white') > # make a pcolor plot. > #x, y = m(lons, lats) > #p = m.pcolor(x,y,maskdat,shading='flat',cmap=cmap) > #clim(*colorbounds) > > # axes units units are left, bottom, width, height > #cax = axes([0.85, 0.1, 0.05, 0.7]) # colorbar axes for map w/ no > graticule > cax = axes([0.88, 0.1, 0.06, 0.81]) # colorbar axes for map w/ graticule > > axes(ax) # make the original axes current again > > ######### Plot symbol at station locations ################# > > #lines=open('file2.txt','r').readlines() > > #(lats,lons)=([],[]) > #for line in lines: > # (lat,lon)=line.strip().split(',') > # lats.append(float(lat)) > # lons.append(float(lon)) > > #for i in range(len(lons)): > # plt.plot(lats,lons,'*') > > #plt.plot(lons,lats,'*') > > #data = csv2rec('file2.txt',delimiter=',') > > #plot(data[:,0],data[:,1],'o') > > #data = csv2rec('file2.txt',delimiter=' ',names=['lat','lon']) > > #plot(data['lat'],data['lon'],'o') > > data = np.loadtxt('file2.txt') > > plot(data[:,0],data[:,1],'o') > > xpt,ypt = m(-75.0,43.0) > text(xpt,ypt,'*') > > > # draw coastlines and political boundaries. > m.drawcoastlines() > m.drawcountries() > m.drawstates() > # draw parallels and meridians. > # label on left, right and bottom of map. > m.drawparallels(parallels,labels=[1,0,0,0]) > m.drawmeridians(meridians,labels=[1,1,0,1]) > > #if plotfile: > # savefig(plotfile, dpi=72, facecolor='w', bbox_inches='tight', > edgecolor='w', orientation='portrait') > #else: > # show() > > #plt.savefig('map.png') > plt.savefig('map.eps') > # comment show to mass produce > > > > > --- On *Tue, 4/19/11, G Jones <gle...@gm...>* wrote: > > > From: G Jones <gle...@gm...> > > Subject: Re: [Matplotlib-users] plotting points/locations from data file > To: "Michael Rawlins" <raw...@ya...> > Cc: "Ian Bell" <ib...@pu...>, Mat...@li... > Date: Tuesday, April 19, 2011, 8:12 PM > > > No need for a header, but I guess my example was a little too simple. You > could do: > data = csv2rec(filename,delimiter=' ',names=['lat','lon']) > plot(data['lat'],data['lon'],'o') > > or you could do > data = np.loadtxt(filename) > plot(data[:,0],data[:,1],'o') > > In general, I strongly recommend developing with ipython --pylab. That way > all the documentation is at your fingertips using the ? and ?? notation. > > |
From: Michael R. <raw...@ya...> - 2011-04-20 01:12:15
|
Thanks Glenn and Ian. I have just explicitly set delimiter=',' but still no points plotted. I'm new to this software so will look into using ipython --pylab and documentation. For the short term I will just create in a shell script 200 strings/records with the lat,lon inserted in the plot command and paste them into the source code. Not ideal but will get the job done. Mike --- On Tue, 4/19/11, G Jones <gle...@gm...> wrote: From: G Jones <gle...@gm...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "Michael Rawlins" <raw...@ya...> Cc: "Ian Bell" <ib...@pu...>, Mat...@li... Date: Tuesday, April 19, 2011, 8:31 PM As you can see from the error message, it's trying to convert "39.4670," to a float and complaining that this is not a valid value (because of the comma. The examples I suggested were for your original space delimited data. For comma delimited data you'll want to remove the delimiter argument to csv2rec (or explicitly set delimiter=',' if you prefer). If you want to use loadtxt, you can set delimiter=',' Again, read the doc strings to see what these functions are expecting by default. On Tue, Apr 19, 2011 at 5:26 PM, Michael Rawlins <raw...@ya...> wrote: The first example produced no plotted symbols but no errors on execution. The second example produced this: Plotting, please wait...maybe more than 10 seconds Traceback (most recent call last): File "testNew.py", line 137, in <module> data = np.loadtxt('file2.txt') File "/usr/lib/python2.6/dist-packages/numpy/lib/io.py", line 489, in loadtxt X.append(tuple([conv(val) for (conv, val) in zip(converters, vals)])) ValueError: invalid literal for float(): 39.4670, Grrrr...... My code follows. I believe this is standard python and matplotlib. Should produce a map of Northeast US. Perhaps someone could get this working with a few example points: 39.4670, -76.1670 46.4000, -74.7670 45.3830, -75.7170 43.6170, -79.3830 45.5170, -73.4170 45.6170, -74.4170 43.8330, -77.1500 43.9500, -78.1670 43.2500, -79.2170 43.8330, -66.0830 #!/usr/bin/env python # v0.5 19 June 2010 # General purpose plotter of 2-D gridded data from NetCDF files, # plotted with map boundaries. # NetCDF file should have data in either 2-D, 3-D or 4-D arrays. # Works with the netCDF files in the tutorial, and also the # files available for download at: # http://www.cdc.noaa.gov/cdc/data.ncep.reanalysis.html # Adapted from the basemap example plotmap_pcolor.py, # Some of the variable names from that example are retained. # # Uses basemap's pcolor function. Pcolor accepts arrays # of the longitude and latitude points of the vertices on the pixels, # as well as an array with the numerical value in the pixel. verbose=0 #verbose=2 says a bit more import sys,getopt from mpl_toolkits.basemap import Basemap, shiftgrid, cm #from netCDF3 import Dataset as NetCDFFile from mpl_toolkits.basemap import NetCDFFile from pylab import * #from matplotlib.mlab import csv2rec alloptions, otherargs= getopt.getopt(sys.argv[1:],'ro:p:X:Y:v:t:l:u:n:') # note the : after o and p proj='lam' #plotfile=None #plotfile='testmap2.png' usejetrev=False colorbounds=[None,None] extratext="" xvar=None yvar=None thevar=None therec=None thelev=None cbot=None ctop=None startlon=-180 #default assumption for starting longitude for theopt,thearg in alloptions: print theopt,thearg if theopt=='-o': # -o needs filename after it, which is now thearg plotfile=thearg elif theopt=='-p': proj=thearg elif theopt=='-X': xvar=thearg elif theopt=='-Y': yvar=thearg elif theopt=='-v': thevar=thearg elif theopt=='-t': thetitle=thearg elif theopt=='-l': cbot=thearg elif theopt=='-u': ctop=thearg elif theopt=='-n': therec=thearg elif theopt=='-m': thelev=thearg elif theopt=='-r': usejetrev=True else: #something went wrong print "hmm, what are these??? ", theopt, thearg sys.exit() print "\nPlotting, please wait...maybe more than 10 seconds" if proj=='lam': #Lambert Conformal m = Basemap(llcrnrlon=-80.6,llcrnrlat=38.4,urcrnrlon=-66.0,urcrnrlat=47.7,\ resolution='l',area_thresh=1000.,projection='lcc',\ lat_1=65.,lon_0=-73.3) xtxt=200000. #offset for text ytxt=200000. parallels = arange(38.,48.,4.) meridians = arange(-80.,-68.,4.) else: #cylindrical is default # m = Basemap(llcrnrlon=-180.,llcrnrlat=-90,urcrnrlon=180.,urcrnrlat=90.,\ # resolution='c',area_thresh=10000.,projection='cyl') m = Basemap(llcrnrlon=startlon,llcrnrlat=-90,urcrnrlon=startlon+360.,urcrnrlat=90.,\ resolution='c',area_thresh=10000.,projection='cyl') xtxt=1. ytxt=0. parallels = arange(-90.,90.,30.) if startlon==-180: meridians = arange(-180.,180.,60.) else: meridians = arange(0.,360.,60.) if verbose>1: print m.__doc__ xsize = rcParams['figure.figsize'][0] fig=figure(figsize=(xsize,m.aspect*xsize)) #ax = fig.add_axes([0.08,0.1,0.7,0.7],axisbg='white') ax = fig.add_axes([0.06,0.00,0.8,1.0],axisbg='white') # make a pcolor plot. #x, y = m(lons, lats) #p = m.pcolor(x,y,maskdat,shading='flat',cmap=cmap) #clim(*colorbounds) # axes units units are left, bottom, width, height #cax = axes([0.85, 0.1, 0.05, 0.7]) # colorbar axes for map w/ no graticule cax = axes([0.88, 0.1, 0.06, 0.81]) # colorbar axes for map w/ graticule axes(ax) # make the original axes current again ######### Plot symbol at station locations ################# #lines=open('file2.txt','r').readlines() #(lats,lons)=([],[]) #for line in lines: # (lat,lon)=line.strip().split(',') # lats.append(float(lat)) # lons.append(float(lon)) #for i in range(len(lons)): # plt.plot(lats,lons,'*') #plt.plot(lons,lats,'*') #data = csv2rec('file2.txt',delimiter=',') #plot(data[:,0],data[:,1],'o') #data = csv2rec('file2.txt',delimiter=' ',names=['lat','lon']) #plot(data['lat'],data['lon'],'o') data = np.loadtxt('file2.txt') plot(data[:,0],data[:,1],'o') xpt,ypt = m(-75.0,43.0) text(xpt,ypt,'*') # draw coastlines and political boundaries. m.drawcoastlines() m.drawcountries() m.drawstates() # draw parallels and meridians. # label on left, right and bottom of map. m.drawparallels(parallels,labels=[1,0,0,0]) m.drawmeridians(meridians,labels=[1,1,0,1]) #if plotfile: # savefig(plotfile, dpi=72, facecolor='w', bbox_inches='tight', edgecolor='w', orientation='portrait') #else: # show() #plt.savefig('map.png') plt.savefig('map.eps') # comment show to mass produce --- On Tue, 4/19/11, G Jones <gle...@gm...> wrote: From: G Jones <gle...@gm...> Subject: Re: [Matplotlib-users] plotting points/locations from data file To: "Michael Rawlins" <raw...@ya...> Cc: "Ian Bell" <ib...@pu...>, Mat...@li... Date: Tuesday, April 19, 2011, 8:12 PM No need for a header, but I guess my example was a little too simple. You could do: data = csv2rec(filename,delimiter=' ',names=['lat','lon']) plot(data['lat'],data['lon'],'o') or you could do data = np.loadtxt(filename) plot(data[:,0],data[:,1],'o') In general, I strongly recommend developing with ipython --pylab. That way all the documentation is at your fingertips using the ? and ?? notation. |
From: Michael R. <raw...@ya...> - 2011-04-20 16:37:46
|
These commands plot points on a map in my code using python, matplotlib, and basemap. Thanks to Ian and Glenn for their assistance. Turns out lat, lon needed to be transformed into Lambert's coordinate space upon which the rest of the map is based. If anyone knows of a more elegant way to work on the entire array, rather than each point, I'll give it a shot. Mike data = csv2rec('file2.txt',delimiter=',',names=['lat','lon']) for i in range(len(data)): x,y=m(data['lon'][i],data['lat'][i]) # Translate to basemap's (Lambert) coordinate space plot(x,y,color='black',marker='.',markersize=6.0) |
From: Heiko B. <hei...@sn...> - 2011-04-20 07:17:12
|
Hi, On Tue, 19 Apr 2011 13:09:23 -0700 (PDT) Michael Rawlins <raw...@ya...> wrote: > I'm reading > the latitudes and longitudes from a file, with each lat, lon pair on > each record (line). I use loadtxt for this purpose. http://www.scipy.org/Cookbook/InputOutput http://www.neuralwiki.org/index.php?title=Loading_data_with_python Heiko -- -- Autoren sollten stehend an einem Pult schreiben. Dann würden -- ihnen ganz von selbst kurze Sätze einfallen. -- (Ernest Hemingway, 1899-1961) -- Number Crunch Blog @ http://numbercrunch.de |