'How to use mpldatacursor for interactive chart

Below is the code that I have wrote and the output

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#from mpldatacursor import datacursor
#%matplotlib nbagg

dfhappyraw = pd.read_csv('F:/gdp-vs-happiness.csv'
                    ,usecols=(0,2,3,4),index_col="Entity",delimiter=',')

#latest amount of data for most filled is in year 2019
dfhappy = dfhappyraw[dfhappyraw["Year"]==2019]

#drop all countries that only has happiness index or gdp
dfhappy = dfhappy.dropna(axis=0)

dfhappy.plot.scatter(x="GDP per capita, PPP (constant 2017 international $)",
                        y="Life satisfaction in Cantril Ladder (World Happiness Report 2021)",color="mediumvioletred",)

plt.title("Scatter Plot for GDP and Self-Reported Life Satisfaction") 
plt.ylabel('Happinees Index (0-10)')
plt.xlabel('GDP')

for idx, row in dfhappy.iterrows(): 
    label = plt.text(row['GDP per capita, PPP (constant 2017 international $)'], 
             row['Life satisfaction in Cantril Ladder (World Happiness Report 2021)'], idx)
#datacursor()

enter image description here

As shown in the output there are too many points to individually label them all.

Found out about mpldatacursor, but I am not really sure how to use it,

I have tried datacursor(formatter=dfhappy.index) but it does not work on the interactive chart



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source