Reputation: 1344
I am trying to associate a motion event to a seaborn heatmap, so that when I hover the mouse in a cell, this cell gets highlighted in the borders. So far, I managed to do it, by associating a motion_notify_event
to the canvas:
import matplotlib.patches as mpatches
self.canvash.mpl_connect('motion_notify_event', self.onMotion)
(...)
def onMotion(self,event):
if not event.inaxes:
return
xint = int(event.xdata)
yint = int(event.ydata)
self.axh.add_patch(mpatches.Rectangle((xint, yint),1,1,fill=False,edgecolor='blue',linewidth=0.5))
self.canvash.draw()
This works, and as I hover the mouse, the cells get highlighted. The problem is that all the cells will get highlighted, and the result is something like this:
I just want to highlight the cell under the mouse as I move it, so that only one is active.
How can I do this?
Upvotes: 0
Views: 1649
Reputation: 1344
I was able to find a solution, thanks to Paul H. I just had to save the patch and then remove it and it works fine, here is the solution:
def onMotion(self,event):
if not event.inaxes:
return
xint = int(event.xdata)
yint = int(event.ydata)
self.rect = mpatches.Rectangle((xint, yint),1,1,fill=False,linestyle='dashed', edgecolor='red',linewidth=2.0)
self.axh.add_patch(self.rect)
self.canvash.draw()
self.rect.remove()
Upvotes: 1