Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Collection's contains method doesn't honour offset_position attribute #1973

Merged
merged 2 commits into from

3 participants

@ChrMa

The contains method of Collection objects doesn't honour the newly introduced _offset_position attribute, making it fail when _offset_position == "data".
I noticed this when trying to add a pick_event handler to a hexbin plot, which does not work properly with matplotlob 1.2.x

@mdboom mdboom was assigned
@mdboom
Owner

@ChrMa: Can you confirm the attached patch fixes your bug?

@pelson
Collaborator

Again - looks good but needs a small test. :+1:

@mdboom mdboom referenced this pull request from a commit in mdboom/matplotlib
@mdboom mdboom Add test for #1973 4674ee2
@ChrMa

@mdboom: Thank you for the quick response. The fix is working perfectly.

@ChrMa

As for the test, you might want to add something like picker=1 to the hexbin call to avoid getting a UserWarning about picker being None.

@mdboom mdboom merged commit eae5e43 into from
@mdboom mdboom deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 3, 2013
  1. @mdboom
  2. @mdboom

    Add test for #1973

    mdboom authored
This page is out of date. Refresh to see the latest.
View
3  lib/matplotlib/collections.py
@@ -302,7 +302,8 @@ def contains(self, mouseevent):
ind = mpath.point_in_path_collection(
mouseevent.x, mouseevent.y, pickradius,
transform.frozen(), paths, self.get_transforms(),
- offsets, transOffset, pickradius <= 0)
+ offsets, transOffset, pickradius <= 0,
+ self.get_offset_position())
return len(ind)>0, dict(ind=ind)
View
18 lib/matplotlib/tests/test_axes.py
@@ -424,6 +424,24 @@ def test_hexbin_extent():
ax.hexbin(x, y, extent=[.1, .3, .6, .7])
+@cleanup
+def test_hexbin_pickable():
+ # From #1973: Test that picking a hexbin collection works
+ class FauxMouseEvent:
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ fig = plt.figure()
+
+ ax = fig.add_subplot(111)
+ data = np.arange(200.)/200.
+ data.shape = 2, 100
+ x, y = data
+ hb = ax.hexbin(x, y, extent=[.1, .3, .6, .7], picker=1)
+
+ assert hb.contains(FauxMouseEvent(400, 300))[0]
+
@image_comparison(baseline_images=['hexbin_log'],
remove_text=True,
extensions=['png'])
View
11 src/_path.cpp
@@ -699,7 +699,7 @@ _path_module::get_path_collection_extents(const Py::Tuple& args)
Py::Object
_path_module::point_in_path_collection(const Py::Tuple& args)
{
- args.verify_length(9);
+ args.verify_length(10);
//segments, trans, clipbox, colors, linewidths, antialiaseds
double x = Py::Float(args[0]);
@@ -711,6 +711,9 @@ _path_module::point_in_path_collection(const Py::Tuple& args)
Py::SeqBase<Py::Object> offsets_obj = args[6];
agg::trans_affine offset_trans = py_to_agg_transformation_matrix(args[7].ptr());
bool filled = Py::Boolean(args[8]);
+ std::string offset_position = Py::String(args[9]);
+
+ bool data_offsets = (offset_position == "data");
PyArrayObject* offsets = (PyArrayObject*)PyArray_FromObject(
offsets_obj.ptr(), PyArray_DOUBLE, 0, 2);
@@ -761,7 +764,11 @@ _path_module::point_in_path_collection(const Py::Tuple& args)
double xo = *(double*)PyArray_GETPTR2(offsets, i % Noffsets, 0);
double yo = *(double*)PyArray_GETPTR2(offsets, i % Noffsets, 1);
offset_trans.transform(&xo, &yo);
- trans *= agg::trans_affine_translation(xo, yo);
+ if (data_offsets) {
+ trans = agg::trans_affine_translation(xo, yo) * trans;
+ } else {
+ trans *= agg::trans_affine_translation(xo, yo);
+ }
}
if (filled)
Something went wrong with that request. Please try again.