I have a Pandas database (below I am creating a random database to mimic mine).
I use Seaborn, facet-grid and scatterplot to plot the data the way I want : Epsilon1 as a function of no, I distinguish the data from the sub categories A and B using different subplots and colors. This part of the code works correctly.
Then I want that the user can click on any dot in order to display in the IPython console and in the status bar of the Matplotlib figure (as here) all the informations about this dot : that is to say all the values of the corresponding dataframe row: something like: 'no':5, 'Date':1997-12-15 03:50:41, 'A':A6, 'B':B4, 'Epsilon1':0.670635, 'Epsilon2':0.756461, 'Epsilon3':0.530825
I have made first tests using onpick event (not shown here) but all were unsuccessful. Actually I can't get by with the function onpick(event) because I do not understand why print(event.ind) gives me a list of integers...
Here is my code
import pandas as pd
import numpy as np
import seaborn as sns
import random
# size of the database
n = 1000
nA = 6
nB = 5
no = np.arange(n)
date = np.random.randint(1e9, size=n).astype('datetime64[s]')
A = [''.join(['A',str(random.randint(1, nA))]) for j in range(n)]
B = [''.join(['B',str(random.randint(1, nB))]) for j in range(n)]
Epsilon1 = np.random.random_sample((n,))
Epsilon2 = np.random.random_sample((n,))
Epsilon3 = np.random.random_sample((n,))
data = pd.DataFrame({'no':no,
'Date':date,
'A':A,
'B':B,
'Epsilon1':Epsilon1,
'Epsilon2':Epsilon2,
'Epsilon3':Epsilon3})
def onpick(event):
print(event.ind)
def plot_Epsilon1_seaborn():
sns.set_theme()
g = sns.FacetGrid(data,
col="A",
col_wrap=4,
hue='B',
hue_order=data['B'].sort_values().drop_duplicates().to_list(),
palette="viridis",
col_order=data['A'].sort_values().drop_duplicates().to_list())
g.map(sns.scatterplot,
'no',
'Epsilon1',
picker=True)
g.add_legend()
g.fig.canvas.mpl_connect("pick_event", onpick)
if __name__ == '__main__':
plot_Epsilon1_seaborn()