'How to show the labels of each scatter point in a basemap?

This is my code:

fig = plt.figure(figsize=(12, 8), dpi=300) 
m = Basemap(width=20000,height=15000,resolution='c',projection='lcc',lon_0=-0.09,lat_0=51.49)
m.drawcoastlines(linewidth=0.5)
m.fillcontinents(color='tan',lake_color='lightblue')
# draw parallels and meridians.
m.drawparallels(np.arange(-90.,91.,15.),labels=[True,True,False,False],dashes=[2,2])
m.drawmeridians(np.arange(-180.,181.,15.),labels=[False,False,False,True],dashes=[2,2])
m.drawmapboundary(fill_color='lightblue')
m.drawcountries(linewidth=2, linestyle='solid', color='k' ) 
m.drawstates(linewidth=0.5, linestyle='solid', color='k')
#m.drawrivers(linewidth=0.5, linestyle='solid', color='blue')

x, y = m(df2['lon'].values, df2['lat'].values)
m.scatter(x,y, marker="*", color='b', alpha=0.7, zorder=5, s=9)


plt.title("Localización de los Bikepoints", fontsize=14)
plt.xlabel('Longitud', fontsize=10)
plt.ylabel('Latitud', fontsize=10)
plt.show()

this is the result: enter image description here

How can I add a label for each point. The names are in a column of my dataframe.



Solution 1:[1]

I did it in this way:

with plt.style.context('seaborn'):      # 'fivethirtyeight'      
    fig = plt.figure(figsize=(20,8)) ;   
    for i in range(nd):
        x = data_df.loc[i,data_df.columns[1]]
        y = data_df.loc[i,data_df.columns[2]]
        tx= data_df.loc[i,data_df.columns[0]]
        plt.plot(x, y, marker='o',ms=29, c='orange',alpha = 0.6)
        plt.text(x,y, tx, fontsize=18)
    plt.title('Nobel Laureates vs Chocolate Consumption', fontweight='bold',fontsize=35)
    plt.margins(0.1)
    plt.show()

Full example here

enter image description here

Solution 2:[2]

I solved this problem as following:

for i in range(df.shape[0]):
    x = df['lon'][i]
    y = df['lat'][i]
    tx= df['location'][i]
    xx, yy = m(x, y) #m is basemap object
    plt.text(xx,yy, tx, fontsize=12)

where:

m = Basemap(projection='lcc', resolution='l',
                lat_0=40.01, lon_0=-1.52,
                width=1.5E6, height=1.1E6)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pyano
Solution 2 Anya Konkina