I have an animated scatter plot which shows GDP per capita x life expectancy per country through the years. I want to add a tooltip that will appear when someone hovers the mouse over a bubble, and I want it to display the name of the country that bubble is equivalent to.
I attempted to use mplcursors to do so, but had trouble figuring out how to display the name of the country, since it's a different one in each bubble.
Here's the plot:
ax.scatter(y = data['lifeExpec'],
x = data['gdp'],
s = data['population']/40000,
c = data['region'].astype('category').cat.codes,
cmap = cm.viridis,
edgecolors = 'none',
alpha = 0.5)
c1 = mplcursors.cursor(ax, hover=True)
@c1.connect("add")
def _(sel):
sel.annotation.set_text(<????>)
sel.annotation.set(position=(-15,15),
fontsize=8,
ha='center',
va='center')
Here's a sample of my dataframe:
country, gdp, lifeExpec, year, population, region
USA, 20000, 70, 2005, 100000, America
USA, 21000, 72, 2007, 104000, America
Canada, 20500, 71, 2005, 50000, America
Canada, 23000, 74, 2007, 53000, America