简体   繁体   中英

plotting point on map in python using basemap

so I am having some problems trying to get some points to plot(or rather show up) on the map.

Here is the code:

#!/usr/bin/python3

import matplotlib.pyplot as plt
import re

from mpl_toolkits.basemap import Basemap

m = Basemap(resolution='i', # c, l, i, h, f or None. crude, low, intermediate, high or full.
        projection='merc',
        lat_0=37.80, lon_0=-96.02,
        llcrnrlon=-125.22,
        llcrnrlat= 24.25,
        urcrnrlon=-66.83,
        urcrnrlat=49.26,
        area_thresh=10000)

m.drawcoastlines()
m.drawcountries()
m.drawstates()
m.drawmapboundary(fill_color='#46bcec')
m.fillcontinents(color='white',lake_color='#46bcec')


latlongs = []
lons = []
lats = []
with open('latlongsLocsNewBingAO.txt', 'r') as in_file:
    for line in in_file:
        line = line.replace('\n', '')
        latlongs.append(line)

for latlong in latlongs:
    if re.match(r'None ,*', latlong) is None:
        latlong = ",".join(latlong.split(",")[:2])
        latlong = latlong.replace('[', '')
        latlong = latlong.replace(']' ,'')
        ftuple = tuple(map(float, latlong.split(', ')))
        xT, yT = ftuple
        lons.append(xT)
        lats.append(yT)


x, y = m(lons, lats)
#print(x, y)
m.scatter (x, y, marker='o', color='r', zorder=5)

plt.show()

and here are some of the points I am pulling from that file that it's pulling from.

[40.9825820922852, -73.8059005737305] ,Scarsdale, New York 10583
[39.8815994262695, -83.0865631103516] ,Grove City, Ohio 43123
[35.4720306396484, -97.5210723876953] ,Oklahoma City, Oklahoma 73115
[29.7605800628662, -95.3696823120117] ,HOUSTON, TEXAS 1073
[32.8589515686035, -96.5462646484375] ,Garland, Texas 75043

no matter what I have tried I cannot get the points to show up on the map. So I would greatly appreciate some help with this. Thank you in advance.

I figured it out, I had my Lats and lons backwards in this line:

x, y = m(lons, lats)

and I should have had it as:

x, y = m(lats, lons)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM