yuliaUU
yuliaUU

Reputation: 1713

how to add multiple point on basemap

 plt.subplots_adjust(left=0.05,right=0.95,top=0.90,bottom=0.05,wspace=0.15,hspace=0.05)
axs = plt.subplot(111)

m = Basemap(llcrnrlon=-50,llcrnrlat=40.2,urcrnrlon=0,urcrnrlat=52.2,
            resolution='i',projection='merc'ax = axs)
m.drawcountries(linewidth=0.5)
m.drawcoastlines(linewidth=0.5)
m.fillcontinents(color="grey")
m.drawmapboundary()

#to plot multiple points

i=1
lons=list()
lats=list()
lbl=list()
while i<15:
    lons=list(df[Lon])
    lats=list(df[Lat])
    lbl=list(df[Site])
    lons.append(i)
    lats.append(i)
    lbl.append(i)
    x,y = m(lons, lats)
    axs.plot(x, y, 'o', label=lbl)
    i+=1

plt.show()

Could you please help me with the loop. df is my data frame. Lon, Lat and Site are the names of the columns in the df.

Upvotes: 0

Views: 1724

Answers (2)

Roger Veciana
Roger Veciana

Reputation: 993

I don't understand why are you creating the lists with longitudes and latitudes, since you are plotting point by point.

Besides, I think that you want to label each point, which can be done with annotate. I would do it like this:

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

plt.subplots_adjust(left=0.05,right=0.95,top=0.90,bottom=0.05,wspace=0.15,hspace=0.05)
axs = plt.subplot(111)

m = Basemap(llcrnrlon=-50,llcrnrlat=40.2,urcrnrlon=0,urcrnrlat=52.2,
            resolution='i',projection='merc', ax = axs)
m.drawcountries(linewidth=0.5)
m.drawcoastlines(linewidth=0.5)
m.fillcontinents(color="grey")
m.drawmapboundary()

df = [{'lon': -8, 'lat': 45, 'site': 'point1'}, 
{'lon': -16, 'lat': 46, 'site': 'point2'}]

for point in df:
    x, y = m(point['lon'], point['lat'])
    axs.annotate(point['site'], xy=(x, y),  xycoords='data',
                xytext=(-40, 20), textcoords = 'offset points',
                arrowprops=dict(arrowstyle="->"))
    axs.plot(x, y, marker='o')

plt.show()

The result

Upvotes: 0

agamike
agamike

Reputation: 517

Use

axs.plot(x, y, 'o', label=lbl)

instead of

m.plot(x, y, 'o', label=lbl)

Upvotes: 1

Related Questions