Here is my Jupyer Notebook source code.
But a hard-coded reproducible example is below. (You will need access to the UK.geojson file from my Github or the true source: http://geoportal1-ons.opendata.arcgis.com/datasets/687f346f5023410ba86615655ff33ca9_1.geojson)
I have a geopandas DataFrame to plot the UK map and then plot certain coordinates (park run locations) on top of it.
I want to be able to interactively 'pick' the coordinates and then I will code it to show me information about that specific run location.
I can't figure out why the fig.canvas.mpl_connect('pick_event', onpick)
function I'm calling does not work.
Please can someone help me figure this out?
import pandas as pd
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
##### Create hardcoded dataset ######
df = pd.DataFrame.from_dict({0: {'parkrun_name': 'Beeston',
'location_name': 'Weirfields Recreation Ground',
'longitude': -1.201737,
'latitude': 52.913592,
'Run Date': '12/11/2022',
'Run Number': 370.0,
'Pos': 107.0,
'Time': '26:05',
'Age Grade': '49.46%',
'PB?': np.nan},
1: {'parkrun_name': 'Colwick',
'location_name': 'Colwick Country Park, Nottingham',
'longitude': -1.09786,
'latitude': 52.945171,
'Run Date': '22/10/2022',
'Run Number': 511.0,
'Pos': 127.0,
'Time': '29:44',
'Age Grade': '43.39%',
'PB?': np.nan},
2: {'parkrun_name': 'Exmouth',
'location_name': 'Exmouth',
'longitude': -3.412392,
'latitude': 50.614697,
'Run Date': '24/12/2022',
'Run Number': 189.0,
'Pos': 197.0,
'Time': '25:44',
'Age Grade': '50.13%',
'PB?': 'PB'},
}, orient='index')
###### Read in the UK Map File ######
uk = gpd.read_file("uk.geojson")
###### Convert df to geodf ######
gdf = gpd.GeoDataFrame(
df, geometry=gpd.points_from_xy(df.longitude, df.latitude)
)
gdf.crs = "EPSG:4326"
gdf = gdf.to_crs(uk.crs)
###### Plot interactive map ######
%matplotlib widget
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
uk.plot(ax=ax, alpha=0.8)
def onclick(event):
ax = plt.gca()
# ax.set_title(f"You selected {event.x}, {event.y}")
line = event.artist
xdata, ydata = line.get_data()
ind = event.ind
tx = f"{np.array([xdata[ind], ydata[ind]]).T}"
ax.set_title(f"{tx}")
gdf[gdf['Run Date'].isna()].plot(ax=ax, color='black', marker='.', markersize=8, alpha=0.2, picker=20)
gdf[gdf['Run Date'].notna()].plot(ax=ax, color='#AAFF00', marker='.', markersize=50, picker=20)
ax.set_xlim(-7,2)
ax.set_ylim(49,60)
cid = fig.canvas.mpl_connect('pick_event', onclick)