Stücke
Stücke

Reputation: 1043

Python basemap with scatter plot

I am trying to plot the location of NYC and Berlin on a basemap with a scatter plot.

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

m = Basemap(projection='robin',lon_0=0,resolution='l')

# New York and Berlin
lats = (13.388889,52.516667)
lons = (-74.0059,40.7127)

m.drawcountries(color='#ffffff', linewidth=0.5)
m.fillcontinents(color='#c0c0c0',lake_color='#ffffff')

x, y = m(lons, lats)  # transform coordinates
plt.scatter(x, y, 10, marker='o', color='Red') 

plt.savefig("filename.svg", figsize=(24,12))
plt.show()

However, only one of the points shows up on the map at the wrong location. enter image description here

Can anyone assisst me and point out why one point shows up at the wrong location and the other dot doe snot show up at all?

Upvotes: 0

Views: 1550

Answers (1)

Alexandre B.
Alexandre B.

Reputation: 5502

The matter is your coordinate definition. You are mixing longitude and latitude. After checking the coordinates of Berlin and New York, it comes the following definition:

# Berlin & New York
lats = [52.516667, 40.730610 ]
lons = [13.388889, -73.935242]

Here the whole code:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

m = Basemap(projection='robin', lon_0=0, resolution='l')

# Berlin & New York
lats = [52.516667, 40.730610 ]
lons = [13.388889, -73.935242]

m.drawcountries(color='#ffffff', linewidth=0.5)
m.fillcontinents(color='#c0c0c0', lake_color='#ffffff')

x, y = m(lons, lats)
plt.plot(x, y, 'bo', color='r', markersize=5)

plt.show()

enter image description here

Upvotes: 2

Related Questions