I am currently trying to program a seaborn stripplot which shows a point’s column and index in the dataframe when hovered on by the mouse.
This raises a few questions:
What does stripplot.contains()
return? I get that it returns a boolean saying whether the event lies in the container-artist and a dict
giving the labels of the picked data points. But what does this dict
actually look like in the case of a 2D DataFrame?
How can I locate a data point in my dataframe thanks to this data?
Thank you for your help.
My current program looks like follows, and is largely taken from this issue:
#Creating the data frame
A = pd.DataFrame(data = np.random.randint(10,size=(5,5)))
fig,ax = plt.subplots()
strp = sns.stripplot(A)
annot = ax.annotate("", xy=(0,0), xytext=(20,20),textcoords="offset points",
bbox=dict(boxstyle="round", fc="w"))
annot.set_visible(False)
def update_annot(ind):
mX, mY = A[ind["ind"][0]], A[ind["ind"][0]].loc[ [ind["ind"][0]] ]
annot.xy = (mX,mY)
annot.set_text(str(ind["ind"][0])+", "+str(ind["ind"][0]))
annot.get_bbox_patch().set_facecolor("blue")
annot.get_bbox_patch().set_alpha(0.4)
def hover(event):
vis = annot.get_visible()
if event.inaxes == ax:
cont, ind = strp.contains(event)
if cont:
update_annot(ind)
annot.set_visible(True)
fig.canvas.draw_idle()
else:
if vis:
annot.set_visible(False)
fig.canvas.draw_idle()
fig.canvas.mpl_connect("motion_notify_event", hover)
plt.show()
My guess here is that there is a problem with what the shape I am expecting from the dict
that stripplot.contains
outputs. And since I can not find a way to print it (once the last line is run, nothing print anymore), it is hard for me to know…
Thank you!