Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eae5e43

Browse files
committed
Merge pull request #1973 from mdboom/pick_path_collection_data_offset_position
Collection's contains method doesn't honour offset_position attribute
2 parents aca3fea + 15432dc commit eae5e43

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

lib/matplotlib/collections.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ def contains(self, mouseevent):
302302
ind = mpath.point_in_path_collection(
303303
mouseevent.x, mouseevent.y, pickradius,
304304
transform.frozen(), paths, self.get_transforms(),
305-
offsets, transOffset, pickradius <= 0)
305+
offsets, transOffset, pickradius <= 0,
306+
self.get_offset_position())
306307

307308
return len(ind)>0, dict(ind=ind)
308309

lib/matplotlib/tests/test_axes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,24 @@ def test_hexbin_extent():
424424

425425
ax.hexbin(x, y, extent=[.1, .3, .6, .7])
426426

427+
@cleanup
428+
def test_hexbin_pickable():
429+
# From #1973: Test that picking a hexbin collection works
430+
class FauxMouseEvent:
431+
def __init__(self, x, y):
432+
self.x = x
433+
self.y = y
434+
435+
fig = plt.figure()
436+
437+
ax = fig.add_subplot(111)
438+
data = np.arange(200.)/200.
439+
data.shape = 2, 100
440+
x, y = data
441+
hb = ax.hexbin(x, y, extent=[.1, .3, .6, .7], picker=1)
442+
443+
assert hb.contains(FauxMouseEvent(400, 300))[0]
444+
427445
@image_comparison(baseline_images=['hexbin_log'],
428446
remove_text=True,
429447
extensions=['png'])

src/_path.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ _path_module::get_path_collection_extents(const Py::Tuple& args)
699699
Py::Object
700700
_path_module::point_in_path_collection(const Py::Tuple& args)
701701
{
702-
args.verify_length(9);
702+
args.verify_length(10);
703703

704704
//segments, trans, clipbox, colors, linewidths, antialiaseds
705705
double x = Py::Float(args[0]);
@@ -711,6 +711,9 @@ _path_module::point_in_path_collection(const Py::Tuple& args)
711711
Py::SeqBase<Py::Object> offsets_obj = args[6];
712712
agg::trans_affine offset_trans = py_to_agg_transformation_matrix(args[7].ptr());
713713
bool filled = Py::Boolean(args[8]);
714+
std::string offset_position = Py::String(args[9]);
715+
716+
bool data_offsets = (offset_position == "data");
714717

715718
PyArrayObject* offsets = (PyArrayObject*)PyArray_FromObject(
716719
offsets_obj.ptr(), PyArray_DOUBLE, 0, 2);
@@ -761,7 +764,11 @@ _path_module::point_in_path_collection(const Py::Tuple& args)
761764
double xo = *(double*)PyArray_GETPTR2(offsets, i % Noffsets, 0);
762765
double yo = *(double*)PyArray_GETPTR2(offsets, i % Noffsets, 1);
763766
offset_trans.transform(&xo, &yo);
764-
trans *= agg::trans_affine_translation(xo, yo);
767+
if (data_offsets) {
768+
trans = agg::trans_affine_translation(xo, yo) * trans;
769+
} else {
770+
trans *= agg::trans_affine_translation(xo, yo);
771+
}
765772
}
766773

767774
if (filled)

0 commit comments

Comments
 (0)