Issue plotting coordinates using Basemap - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Issue plotting coordinates using Basemap (/thread-2345.html) |
Issue plotting coordinates using Basemap - kiton - Mar-08-2017 Having extracted geo coordinates from Twitter JSON, I created a new pandas data frame: df3 Out[140]: lat lon pos 63 -26.298376 28.070223 (-26.29837597, 28.07022309) 116 1.487180 124.861180 (1.48718, 124.86118) 199 33.708842 -84.294066 (33.70884247, -84.29406577) 349 35.368816 -96.955488 (35.36881649, -96.95548791) 379 14.830463 120.284751 (14.83046326, 120.2847507) 859 35.151530 -80.727347 (35.15153029, -80.72734744)Now, my goal is to plot the coordinates on the map using Basemap: import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap # Set map size fig, ax = plt.subplots(figsize=(10,20)) # Set USA coordinates m = Basemap(resolution='c', # c, l, i, h, f or None projection='merc', lat_0=39, lon_0=-99, ## center of the map llcrnrlon=-125, llcrnrlat= 25, urcrnrlon=-65, urcrnrlat=49) # Read county boundaries shp_info = m.readshapefile('cb_2015_us_county_500k', 'counties', drawbounds=True) # Set geo coordinates to be plotted lats = df3['lat'] [:] #all values from column lat lons = df3['lon'] [:] #all values from column lon x,y = m(lats, lons) # Place points on the map m.plot(x, y, 'ro', markersize=5) m.bluemarble() plt.show()However, for some reason I get this error: Can anybody please tell me where I am making a mistake.
RE: Issue plotting coordinates using Basemap - zivoni - Mar-08-2017 I have no experience with mpl_toolkits, but considering that it is package associated with matplotlib, I wonder what happens if you try x, y = m(lats.values, lons.values) # instead of x,y=m(lats, lons) RE: Issue plotting coordinates using Basemap - kiton - Mar-08-2017 It did work! Good catch, sir :) The result (see attached), however, presents a map with county grid but without coordinates plotted. And the following error. Weird, isn't it?
RE: Issue plotting coordinates using Basemap - zivoni - Mar-09-2017 It is not an error, just a warning about using deprecated function. It seems that some internal function in basemap uses it and according to matplotlib issues it is already patched, so it will disappear after next update. For now you can ignore it. I downloaded mpl_toolkits and tried it with your code and fictional data. This code: # Place points on the map x, y = m(np.array([-104, -100, -90]), np.array([40, 42, 38])) m.plot(x, y, 'ro', markersize=15)seems to be working to plot locations, so maybe you need to check your coordinate's values? [Image: bgBHUtJ.png] RE: Issue plotting coordinates using Basemap - kiton - Mar-09-2017 I switched the coordinates like: x, y = m(lons.values, lats.values) # instead of x, y = m(lats.values, lons.values)...and its all there :) Thanks a lot for help again! I'll double-check how the values were recorded in the data frame. RE: Issue plotting coordinates using Basemap - Larz60+ - Mar-09-2017 Just a suggestion (if you can do this) make the line thickness a bit thinner and white (or at least lighter color) for his view. In order to work right, that may have to be adjusted when zooming in. |