Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14185

Cartopy Select any country with a mouse click

$
0
0

I need your help with this. I want to left click on a country, ex Italy. Print the name Italy and color Italy on the map. Should be usable with any country. Here is my code:

import matplotlib.pyplot as pltimport cartopyimport cartopy.io.shapereader as shpreaderimport cartopy.crs as ccrsax = plt.axes(projection=ccrs.PlateCarree())ax.add_feature(cartopy.feature.LAND)ax.add_feature(cartopy.feature.OCEAN)ax.add_feature(cartopy.feature.COASTLINE)ax.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=.5)ax.add_feature(cartopy.feature.LAKES, alpha=0.95)#ax.add_feature(cartopy.feature.RIVERS)ax.set_extent([-150, 60, -25, 60])shpfilename = shpreader.natural_earth(resolution='110m',                                      category='cultural',                                      name='admin_0_countries')reader = shpreader.Reader(shpfilename)countries = reader.records()for country in countries:    if country.attributes['NAME_LONG'] == 'Italy':        ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(0, 1, 0), label = "A")    else:        ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(1, 1, 1), label = country.attributes['NAME_LONG'])def onclick(event):    x,y = event.xdata, event.ydata    print(x, y)plt.connect('button_press_event', onclick)plt.rcParams["figure.figsize"] = (50,50)plt.show()

Viewing all articles
Browse latest Browse all 14185

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>