Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/matplotlib/collections.py
Expand Up @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Expand Up @@ -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'])
Expand Down
11 changes: 9 additions & 2 deletions src/_path.cpp
Expand Up @@ -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]);
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down