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

Fixed categorical coloring of Contours in matplotlib #2259

Merged
merged 2 commits into from
Feb 5, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion holoviews/core/data/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member Author

@philippjfr philippjfr Feb 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the most bizarre bug I've come across in a long time. Before I added this dtype approach this line would error (but only on Travis), given this input:np.full(10, 'B'), saying that:

ValueError: could not convert string to float: 'B'

But internally np.full does exactly the same thing I've now done here to fix it:

def full(shape, fill_value, dtype=None, order='C'):
    if dtype is None:
        dtype = array(fill_value).dtype
    a = empty(shape, dtype, order)
    multiarray.copyto(a, fill_value, casting='unsafe')
    return a

Absolutely baffling, but it seems to work now.

else:
if not expanded:
return util.unique_array(values)
Expand Down
7 changes: 5 additions & 2 deletions holoviews/plotting/mpl/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {}
Expand Down
9 changes: 9 additions & 0 deletions tests/testplotinstantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down