Skip to content

Commit

Permalink
Fixed unclosed paths on polygons (#3477)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Feb 9, 2019
1 parent 02cf145 commit 745e5bd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ jobs:
- popd
- mv doc/Tutorials/.coverage ./.coverage
- coveralls
after_failure:
- popd
- sleep 10

- <<: *regression_test
env: DESC="Python 2.7 regression tests" PYENV_VERSION=2.7 HV_REQUIREMENTS="nbtests"
Expand Down
9 changes: 8 additions & 1 deletion holoviews/plotting/mpl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ def ring_coding(array):
n = len(array)
codes = np.ones(n, dtype=Path.code_type) * Path.LINETO
codes[0] = Path.MOVETO
codes[-1] = Path.CLOSEPOLY
return codes


Expand All @@ -338,7 +339,13 @@ def polygons_to_path_patches(element):
for j, array in enumerate(arrays):
if j != (len(arrays)-1):
array = array[:-1]
interiors = holes[i][j] if has_holes else []
if (array[0] != array[-1]).any():
array = np.append(array, array[:1], axis=0)
interiors = []
for interior in (holes[i][j] if has_holes else []):
if (interior[0] != interior[-1]).any():
interior = np.append(interior, interior[:1], axis=0)
interiors.append(interior)
vertices = np.concatenate([array]+interiors)
codes = np.concatenate([ring_coding(array)]+
[ring_coding(h) for h in interiors])
Expand Down
16 changes: 8 additions & 8 deletions holoviews/tests/plotting/matplotlib/testpathplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ def test_polygon_with_hole_plot(self):
self.assertEqual(len(paths), 1)
path = paths[0]
self.assertEqual(path.vertices, np.array([
(1, 2), (2, 0), (3, 7), (1.5, 2), (2, 3), (1.6, 1.6),
(2.1, 4.5), (2.5, 5), (2.3, 3.5)])
(1, 2), (2, 0), (3, 7), (1, 2), (1.5, 2), (2, 3), (1.6, 1.6),
(1.5, 2), (2.1, 4.5), (2.5, 5), (2.3, 3.5), (2.1, 4.5)])
)
self.assertEqual(path.codes, np.array([1, 2, 2, 1, 2, 2, 1, 2, 2]))
self.assertEqual(path.codes, np.array([1, 2, 2, 79, 1, 2, 2, 79, 1, 2, 2, 79]))

def test_multi_polygon_hole_plot(self):
xs = [1, 2, 3, np.nan, 6, 7, 3]
Expand All @@ -98,13 +98,13 @@ def test_multi_polygon_hole_plot(self):
self.assertEqual(len(paths), 2)
path = paths[0]
self.assertEqual(path.vertices, np.array([
(1, 2), (2, 0), (3, 7), (1.5, 2), (2, 3), (1.6, 1.6),
(2.1, 4.5), (2.5, 5), (2.3, 3.5)])
(1, 2), (2, 0), (3, 7), (1, 2), (1.5, 2), (2, 3), (1.6, 1.6),
(1.5, 2), (2.1, 4.5), (2.5, 5), (2.3, 3.5), (2.1, 4.5)])
)
self.assertEqual(path.codes, np.array([1, 2, 2, 1, 2, 2, 1, 2, 2]))
self.assertEqual(path.codes, np.array([1, 2, 2, 79, 1, 2, 2, 79, 1, 2, 2, 79]))
path2 = paths[1]
self.assertEqual(path2.vertices, np.array([(6, 7), (7, 5), (3, 2)]))
self.assertEqual(path2.codes, np.array([1, 2, 2]))
self.assertEqual(path2.vertices, np.array([(6, 7), (7, 5), (3, 2), (6, 7)]))
self.assertEqual(path2.codes, np.array([1, 2, 2, 79]))

def test_polygons_color_op(self):
polygons = Polygons([
Expand Down
36 changes: 36 additions & 0 deletions holoviews/tests/plotting/matplotlib/testutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import absolute_import, unicode_literals

import numpy as np

from holoviews.element import Polygons
from holoviews.plotting.mpl.util import polygons_to_path_patches

from .testplot import TestMPLPlot


class TestUtils(TestMPLPlot):

def test_polygon_to_path_patches(self):
xs = [1, 2, 3, np.nan, 6, 7, 3, np.nan, 0, 0, 0]
ys = [2, 0, 7, np.nan, 7, 5, 2, np.nan, 0, 1, 0]

holes = [
[[(1.5, 2), (2, 3), (1.6, 1.6)], [(2.1, 4.5), (2.5, 5), (2.3, 3.5)]],
[],
[]
]
polys = Polygons([{'x': xs, 'y': ys, 'holes': holes}])
paths = polygons_to_path_patches(polys)


self.assertEqual(len(paths), 1)
self.assertEqual(len(paths[0]), 3)
self.assertEqual(paths[0][0].get_path().vertices, np.array([
(1, 2), (2, 0), (3, 7), (1, 2),
(1.5, 2), (2, 3), (1.6, 1.6), (1.5, 2),
(2.1, 4.5), (2.5, 5), (2.3, 3.5), (2.1, 4.5)]))
self.assertEqual(paths[0][0].get_path().codes, np.array([1, 2, 2, 79, 1, 2, 2, 79, 1, 2, 2, 79], dtype='uint8'))
self.assertEqual(paths[0][1].get_path().vertices, np.array([(6, 7), (7, 5), (3, 2), (6, 7)]))
self.assertEqual(paths[0][1].get_path().codes, np.array([1, 2, 2, 79], dtype='uint8'))
self.assertEqual(paths[0][2].get_path().vertices, np.array([(0, 0), (0, 1), (0, 0)]))
self.assertEqual(paths[0][1].get_path().codes, np.array([1, 2, 2, 79], dtype='uint8'))

0 comments on commit 745e5bd

Please sign in to comment.