From 69d78970d71f88bf798b390ba778a274e29aa08c Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sat, 15 Feb 2020 21:55:28 +0000 Subject: [PATCH 1/2] Avoid single_path_optimization --- lib/matplotlib/collections.py | 8 ++++++++ lib/matplotlib/tests/test_collections.py | 24 +++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 3b4c06cf93aa..e6d747301f4d 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -345,6 +345,14 @@ def draw(self, renderer): and extents.height < self.figure.bbox.height): do_single_path_optimization = True + # The single path optimization code path is currently broken when + # offsets are specified + # (https://github.com/matplotlib/matplotlib/issues/16496) + # This should be fixed eventually, but for now don't use the optimized + # code path if some of the offsets are non-zero + if not np.all(offsets == 0) and self._offset_position == 'data': + do_single_path_optimization = False + if self._joinstyle: gc.set_joinstyle(self._joinstyle) diff --git a/lib/matplotlib/tests/test_collections.py b/lib/matplotlib/tests/test_collections.py index 75cdbf0ce7c5..168bc864f0ce 100644 --- a/lib/matplotlib/tests/test_collections.py +++ b/lib/matplotlib/tests/test_collections.py @@ -13,7 +13,7 @@ import matplotlib.collections as mcollections import matplotlib.transforms as mtransforms from matplotlib.collections import Collection, LineCollection, EventCollection -from matplotlib.testing.decorators import image_comparison +from matplotlib.testing.decorators import image_comparison, check_figures_equal def generate_EventCollection_plot(): @@ -612,3 +612,25 @@ def test_EventCollection_nosort(): arr = np.array([3, 2, 1, 10]) coll = EventCollection(arr) np.testing.assert_array_equal(arr, np.array([3, 2, 1, 10])) + + +@check_figures_equal() +def test_collection_offset(fig_test, fig_ref): + polygon = np.array([[-1, -1], [-1, 1], [1, 1], [1, -1]]) * 0.1 + offsets = np.array([2, 2]) + + # Add the collection off the edge of the axes, so if offsets is applied + # correctly it is not within the axes bounds + ax = fig_test.subplots() + # edgecolors and linewidths must be set to trigger the right code path + collection = mcollections.PolyCollection( + [polygon], + edgecolors='face', + linewidths=[1], + offsets=offsets, + transOffset=mtransforms.IdentityTransform(), + offset_position="data") + ax.add_collection(collection) + + # Create an empty figure + ax = fig_ref.subplots() From b54e0dee97d1abd159e94d00a1033b7aa4ea6b5b Mon Sep 17 00:00:00 2001 From: David Stansby Date: Thu, 19 Mar 2020 17:10:06 +0000 Subject: [PATCH 2/2] Update lib/matplotlib/collections.py Co-Authored-By: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/collections.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index e6d747301f4d..21a40737437d 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -350,7 +350,7 @@ def draw(self, renderer): # (https://github.com/matplotlib/matplotlib/issues/16496) # This should be fixed eventually, but for now don't use the optimized # code path if some of the offsets are non-zero - if not np.all(offsets == 0) and self._offset_position == 'data': + if np.any(offsets != 0) and self._offset_position == 'data': do_single_path_optimization = False if self._joinstyle: