Reputation: 469
I have an excel file with the information of some cites, now I want to plot those cites with their name on the map. I wrote the following codes, however, I can't get the name of the cites properly. For each cite I get a label with the whole list of 'name'.Any suggestion is welcome.
#I could not install PyGMT or basemap or even geopandas,so I just have cartopy.
import pandas as pd
import csv
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
df = pd.read_excel('./seismogram.xlsx')
lon = df['longitude'].to_list()
lat = df['latitude '].to_list()
name = (df['code']).to_list()
def main():
fig = plt.figure(figsize=(15,15))
plt.rcParams["font.size"] = 18
ax = fig.add_subplot(1,1,1, projection=ccrs.PlateCarree())
ax.set_extent([128, 133, 30, 35], crs=ccrs.PlateCarree())
ax.set_title("Japan")
ax.coastlines(resolution='10m')
ax.add_feature(cfeature.BORDERS, linestyle=':')
ax.stock_img()
return fig, ax
fig, ax = main()
ax.scatter(lon, lat, color="r", marker="o", s = 15)
zip_object = zip(lon, lat, name)
for (lg, lt, ne) in zip_object:
ax.text(lg - .05, lt + .05,
name,
va='center',
ha='right', transform=ccrs.Geodetic(), fontweight='bold')
plt.show()
Upvotes: 0
Views: 1429
Reputation: 615
From your for-loop, I'm 99% certain you should be using ne
in place of name
:
zip_object = zip(lon, lat, name)
for (lg, lt, ne) in zip_object:
ax.text(lg - .05, lt + .05,
ne,
va='center',
ha='right', transform=ccrs.Geodetic(), fontweight='bold')
Easy mistake to make :)
Upvotes: 2