joferkington / mpldatacursor

Interactive "data cursors" (a.k.a. annotation pop-ups) for matplotlib
MIT License
194 stars 47 forks source link

Rectangle artist label annotation returns None #44

Closed bruno2git closed 8 years ago

bruno2git commented 9 years ago

Hi, Joe Kington, Thank you so much for creating this very useful tool! It has helped me a lot!

About the issue, I have used mpldatacursor to add annotation boxes to Rectangle artists with their labels. Since I uppdated mpldatacursor to version 0.6. all annotations return None. A very simple exemple:

from matplotlib import pyplot as plt
from mpldatacursor import datacursor
fig = plt.figure()
ax = fig.add_subplot(111)
rect1 = plt.Rectangle((-200,-100), 400, 200, color='yellow', label='rect1')
rect2 = plt.Rectangle((0,150), 300, 20, color='red', label='rect2')
rect3 = plt.Rectangle((-300,-50), 40, 200, color='#0099FF', label='rect3')
circle = plt.Circle((-200,-250), radius=90, color='#EB70AA', label='circle')
ax.add_patch(rect1)
ax.add_patch(rect2)
ax.add_patch(rect3)
ax.add_patch(circle)
plt.xlim([-400, 400])
plt.ylim([-400, 400])
datacursor(formatter='{label}'.format)
plt.show()

In an environment with mpldatacursor version 0.5 it shows the labels. With version 0.6.0 or 0.6.1 it returns None. (Note that the circle label appears in both versions.)

If in _pickinfo.py, in rectangle_props (line 287) you raise the AttributeError, it prints AttributeError: 'Rectangle' object has no attribute '_mpldatacursor_label'

These are probably hacks but, If you replace label = artist._mpldatacursor_label with label = artist.get_label() (line 285) it works again. This is also true if you remove (comment out) the Rectangle from the registry (datacursor.py, line 306, in event_info) #Rectangle : [pick_info.rectangle_props].

Regards, Bruno

joferkington commented 9 years ago

Thanks for the very clear bug report, @bruno2git!

At first glance, it looks like it's due to some workarounds I added for the way rectangles added with plt.bar behave. I didn't think through what happens when they're added directly with Rectangle.

I think it should be an easy fix, but it may be a bit before I'm able to find time to do it. Things are a bit hectic at the moment, but I'll try to get to it soon.

Thanks again!

bruno2git commented 9 years ago

Thank you for the attention this issue may deserve of you, @joferkington! Best regards, Bruno