From 263ae6a420f94636d26578cf03aac928e85da15c Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Mon, 5 Feb 2018 12:11:51 +0000 Subject: [PATCH] Fixed categorical coloring of Contours in matplotlib (#2259) --- holoviews/core/data/dictionary.py | 2 +- holoviews/plotting/mpl/path.py | 7 +++++-- tests/testplotinstantiation.py | 9 +++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/holoviews/core/data/dictionary.py b/holoviews/core/data/dictionary.py index b8ae1d0093..e0f9e827d0 100644 --- a/holoviews/core/data/dictionary.py +++ b/holoviews/core/data/dictionary.py @@ -213,7 +213,7 @@ def values(cls, dataset, dim, expanded=True, flat=True): if np.isscalar(values): if not expanded: return np.array([values]) - values = np.full(len(dataset), values) + values = np.full(len(dataset), values, dtype=np.array(values).dtype) else: if not expanded: return util.unique_array(values) diff --git a/holoviews/plotting/mpl/path.py b/holoviews/plotting/mpl/path.py index 0be0fb855c..33757a515c 100644 --- a/holoviews/plotting/mpl/path.py +++ b/holoviews/plotting/mpl/path.py @@ -85,9 +85,12 @@ def get_data(self, element, ranges, style): return (paths,), style, {} if element.level is not None: - style['array'] = np.full(len(paths), element.level) + array = np.full(len(paths), element.level) else: - style['array'] = element.dimension_values(cdim, expanded=False) + array = element.dimension_values(cdim, expanded=False) + if array.dtype.kind not in 'if': + array = np.searchsorted(np.unique(array), array) + style['array']= array self._norm_kwargs(element, ranges, style, cdim) style['clim'] = style.pop('vmin'), style.pop('vmax') return (paths,), style, {} diff --git a/tests/testplotinstantiation.py b/tests/testplotinstantiation.py index 29ab27d5b3..b76adcd813 100644 --- a/tests/testplotinstantiation.py +++ b/tests/testplotinstantiation.py @@ -341,6 +341,15 @@ def test_image_listed_cmap(self): self.assertIsInstance(cmap, ListedColormap) self.assertEqual(cmap.colors, colors) + def test_contours_categorical_color(self): + path = Contours([{('x', 'y'): np.random.rand(10, 2), 'z': cat} + for cat in ('B', 'A', 'B')], + vdims='z').opts(plot=dict(color_index='z')) + plot = mpl_renderer.get_plot(path) + artist = plot.handles['artist'] + self.assertEqual(artist.get_array(), np.array([1, 0, 1])) + + class TestBokehPlotInstantiation(ComparisonTestCase):