I am trying to create a flight route map using cartopy. I have to add the destination names on the map and to achieve it i am using this code:
origin_lat = 59.41329956
origin_lon= 24.83279991
data = pd.read_csv("merged.csv", skiprows=[1])
This csv file has few columns and one column is called IATA from where I am trying to extract those location strings.
for i in range(len(data)):
lon = data['Longitude'][i]
lat = data['Latitude'][i]
label = data['IATA'][i]
plt.plot([origin_lon, lon], [origin_lat, lat],
color='red', linewidth=1,
transform=ccrs.Geodetic(),
)
print(label) # when i use only print it shows all the strings available in IATA column
plt.text(lon, lat, label[i],
horizontalalignment='right',
transform=ccrs.Geodetic())
But when I plot in map it shows the error
[36 rows x 15 columns]
AMS
ATH
TXL
BRU
Traceback (most recent call last):
File "D:\spyderPython\hw3\readCSV.py", line 65, in <module>
plt.text(lon, lat, label[i],
IndexError: string index out of range
can someone advise please what i am doing wrong?
You are trying to get the label again here:
plt.text(lon, lat, label[i],
horizontalalignment='right',
transform=ccrs.Geodetic())
which I assume your label
is already a string, and range(len(data))
is bigger than the len of your label
. That is why you get an IndexError, simply switch that code with this:
plt.text(lon, lat, label,
horizontalalignment='right',
transform=ccrs.Geodetic())