dhyams/matplotlib — pull request #1 by dhyams (2011-10-27 02:01:01+00:00)
Title: Added a contains() method to matplotlib.legend.Legend().  It was missing previously.

If contains() was called on a legend, the following message is printed:

C:\Python26\lib\site-packages\matplotlib\artist.py:272: UserWarning: 'Legend' needs 'contains' method
warnings.warn("'%s' needs 'contains' method" % self.__class__.__name__)

This small patch gives the Legend() a legitimate contains() method.

The following is a small test script:

#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np

fig = plt.gcf()

t = np.arange(0.0,3.0,0.01)
s = np.sin(2*np.pi*t)
c = np.cos(2*np.pi*t)
plt.plot(t,s,label="sine")
plt.plot(t,c,label="cosine")
leg = plt.gca().legend()
leg.draggable()

def OnMotion(event):
global leg
print leg.contains(event)

cid = fig.canvas.mpl_connect('motion_notify_event',OnMotion)
plt.show()