如何从 csv 文件绘制站名(文本)到地图
How to plot station names (text) from csv file to map
我有一个带有站点位置的绘制地图(来自 CSV),但我发现很难使用附加的 CSV 文件第一列中称为“STATIONS”的站点名称向这些点添加文本。
下面是到目前为止编译的代码。谢谢
'''
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as feature
import cartopy.io.shapereader as shapereader
from cartopy.mpl.ticker import LongitudeFormatter,
LatitudeFormatter
import pandas as pd
countries = shapereader.natural_earth(resolution='10m',
category='cultural',
name='admin_0_countries')
# Find the Nigeria boundary polygon.
for country in shapereader.Reader(countries).records():
if country.attributes['SU_A3'] == 'NGA':
nigeria = country.geometry
break
else:
raise ValueError('Unable to find the NGA boundary.')
plt.figure(figsize=(20, 10))
ax_map = plt.axes(projection=ccrs.PlateCarree())
ax_map.set_extent([-1, 19, -1, 17], ccrs.PlateCarree())
#ax_map.coastlines()
ax_map.add_feature(feature.COASTLINE, linewidth=.5)
ax_map.add_geometries([nigeria], ccrs.PlateCarree(), edgecolor='0.8',
facecolor='none')
grid_lines = ax_map.gridlines(draw_labels=True)
grid_lines.top_labels = False
grid_lines.right_labels = False
lon_formatter = LongitudeFormatter(zero_direction_label=True)
lat_formatter = LatitudeFormatter()
ax_map.xaxis.set_major_formatter(lon_formatter)
ax_map.yaxis.set_major_formatter(lat_formatter)
df = pd.read_csv("met_ngstation.csv")
plt.scatter(df['LONG'],df['LAT'],
color='red', marker='.', transform=ccrs.PlateCarree())
#plt.savefig('coastlines_ng.pdf')
#plt.savefig('coastlines_ng.png')
plt.show()
'''
Link 到 CSV 文件:https://drive.google.com/file/d/1_0b7z7WFfrs5eKY3t7-tykRoZ70Vqjkn/view?usp=sharing
您可以使用以下功能在地块上标记站点。
def labelLocation(x, y, val, ax):
dt = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
for i, point in dt.iterrows():
ax.text(point['x'], point['y'], str(point['val']))
labelLocation(df['LONG'], df['LAT'], df['STATIONS'], plt)
我有一个带有站点位置的绘制地图(来自 CSV),但我发现很难使用附加的 CSV 文件第一列中称为“STATIONS”的站点名称向这些点添加文本。 下面是到目前为止编译的代码。谢谢
'''
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as feature
import cartopy.io.shapereader as shapereader
from cartopy.mpl.ticker import LongitudeFormatter,
LatitudeFormatter
import pandas as pd
countries = shapereader.natural_earth(resolution='10m',
category='cultural',
name='admin_0_countries')
# Find the Nigeria boundary polygon.
for country in shapereader.Reader(countries).records():
if country.attributes['SU_A3'] == 'NGA':
nigeria = country.geometry
break
else:
raise ValueError('Unable to find the NGA boundary.')
plt.figure(figsize=(20, 10))
ax_map = plt.axes(projection=ccrs.PlateCarree())
ax_map.set_extent([-1, 19, -1, 17], ccrs.PlateCarree())
#ax_map.coastlines()
ax_map.add_feature(feature.COASTLINE, linewidth=.5)
ax_map.add_geometries([nigeria], ccrs.PlateCarree(), edgecolor='0.8',
facecolor='none')
grid_lines = ax_map.gridlines(draw_labels=True)
grid_lines.top_labels = False
grid_lines.right_labels = False
lon_formatter = LongitudeFormatter(zero_direction_label=True)
lat_formatter = LatitudeFormatter()
ax_map.xaxis.set_major_formatter(lon_formatter)
ax_map.yaxis.set_major_formatter(lat_formatter)
df = pd.read_csv("met_ngstation.csv")
plt.scatter(df['LONG'],df['LAT'],
color='red', marker='.', transform=ccrs.PlateCarree())
#plt.savefig('coastlines_ng.pdf')
#plt.savefig('coastlines_ng.png')
plt.show()
'''
Link 到 CSV 文件:https://drive.google.com/file/d/1_0b7z7WFfrs5eKY3t7-tykRoZ70Vqjkn/view?usp=sharing
您可以使用以下功能在地块上标记站点。
def labelLocation(x, y, val, ax):
dt = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
for i, point in dt.iterrows():
ax.text(point['x'], point['y'], str(point['val']))
labelLocation(df['LONG'], df['LAT'], df['STATIONS'], plt)