如何在底图中显示每个散点的标签?

2024-04-26 03:17:41 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码:

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()

结果如下: enter image description here

如何为每个点添加标签。这些名称在我的数据帧的一列中。你知道吗


Tags: falsetruelabelsnppltlightbluecolorlon
1条回答
网友
1楼 · 发布于 2024-04-26 03:17:41

我是这样做的:

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()

完整示例here

enter image description here

相关问题 更多 >

    热门问题