Incorrect color mapping using categories=True
for non-linear categories
#5994
Labels
bug
Uh-oh! Something isn't working as expected.
Describe the bug, what's wrong, and what you expected.
When plotting categorical data with a non-linear or non-sequential integer mapping, the categories are not colored correctly.
Edit: For context, this bug means that the use of
categories=True
should probably be avoided until this is fixed, unless it's certain that the categories in the data being plotted are sequential, contiguous, 0-indexed integers. E.g any examples in the docs which currently use thedownload_frog_tissue
ordownload_whole_body_ct
and plotcategorical=True
are likely showing the segmentation labels incorrectly.Steps to reproduce the bug.
E.g. Plot mesh with three categories for cell data: [0, 2, 8]
Will produce this, which shows 3 colors in the scalar bar for 3 categories, but visually only colors the mesh with two.
Setting
categories=False
, we can see the "correct" coloring for the sphere (with non-categorical scalar bar). I would have expected this to as the coloring withcategories=True
, however.I suspect the issue has something to do with this line:
pyvista/pyvista/plotting/lookup_table.py
Line 762 in 3accbf0
which applies a linear mapping for the colors. In this case, the
apply_cmap
function has limited information about the data, as it only receives ann_values
parameter. To resolve this issue, I thinkapply_cmap
needs to know the actual unique values in the data so that it can create a non-linear mapping based on the actual data values, rather than just the number of values in the data.For reference, the value of
n_values
which is passed toapply_cmap
, is set here whencategories=True
:pyvista/pyvista/plotting/plotter.py
Lines 4269 to 4270 in 3accbf0
EDIT: A non-linear mapping applied on its own may result in abnormally sized color widths in the scalar bar, e.g. the scalar bar might look like this for the example above
[0 0 0 0 0 0 0 0 7 8]
with the0
color being disproportionally wide. In this case, perhaps it's better to keep the linear mapping, but only set each category to a single color value, and set all other values in the lookup table to NaN and use SetNanColor to format the colors for categories not present in range. E.g., format the color bar as[0 nan nan nan nan nan nan 7 8]
System Information
Screenshots
No response
The text was updated successfully, but these errors were encountered: