Skip to content

[Bug]: integer colours for pcolorfast / quadmesh #22236

Closed

Description

@2sn

Bug summary

I get an error

ValueError: RGBA values should be within 0-1 range

when passing a byte/integer array to pcolorfast to code the colors as RGBA. It also fails when data type is uint8

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.cm import get_cmap
cmap = get_cmap('bwr_r'')
fig, ax = plt.subplots()
x, y = np.mgrid[0:10:100j, 0:10:100j]
v = np.abs(np.sin(x) * np.cos(y))
c = (cmap(v[:-1, :-1]) * 255).astype(np.int64)
ax.pcolorfast(x, y, c)

Actual outcome

ValueError: RGBA values should be within 0-1 range

Expected outcome

a plot in in some bluish colour

Additional information

fixes:

  1. in colors.py, line 321:
if (isinstance(c, np.ndarray) and c.dtype.kind in "if"

should be replaced by

if (isinstance(c, np.ndarray) and c.dtype.kind in "ifu"

to allow for unsigned int values as well

  1. in line 343:
        if np.any((result < 0) | (result > 1)):
            raise ValueError("RGBA values should be within 0-1 range")

should be replaced by a test including dtype.kind - for 'i' and 'u'.
It may be sufficient to comment it out as a quick fix as it is definitively more broken having it in.

       if c.dtype.kind in "f" and np.any((result < 0) | (result > 1)):
           raise ValueError("RGBA float values should be within 0-1 range")
       if c.dtype.kind in "ui" and np.any((result < 0) | (result > 255)):
           raise ValueError("RGBA fixed values should be within 0-255 range")

even with this it does not quite work

Operating system

5.15.13-200.fc35.x86_64

Matplotlib Version

3.5.1

Matplotlib Backend

gtk3

Python version

3.10.1

Jupyter version

8.0.0

Installation

pip

Activity

  1. jklymak commented on Jan 15, 2022

    @jklymak
    Member

    Matplotlib works using the 0-1 convention, not 0-255. https://matplotlib.org/stable/api/colors_api.html#module-matplotlib.colors

  2. 2sn commented on Jan 15, 2022

    @2sn
    Author

    I seem to recall it used to support this format in the for color specification in the past.
    This is also the reason, I believe, the dtype.kind - "i" was in the first code piece I quoted.

  3. jklymak commented on Jan 15, 2022

    @jklymak
    Member

    That's not impossible, but if it did, that was a very ambiguous api. Floats having one scaling and integers another is confusing and prone to errors. I'm against any special magic to make this work.

  4. 2sn commented on Jan 15, 2022

    @2sn
    Author

    the byte value is also what image data usually uses, e.g., Pillow. I think it would not be the worst idea having a consistent interface. As I described above, some parts of it are in the code, maybe as a plan, maybe historic left-over. I don;t think it would be a big change, but matplotlib code has grown too much of a jungle for me to work though in a few hours, better for someone to look into who is familiar with the current structures and interdependencies.

    I failed to get pcolorfast to work with current cartopy. It just returns a blank image. Any ideas? Maybe some of their codes is not prepared for the extra dimension.

  5. anntzer commented on Jan 15, 2022

    @anntzer
    Contributor

    Matplotlib works using the 0-1 convention, not 0-255.

    Actually imshow() supports 0-255 int inputs as well, so I think it would be reasonable for pcolorfast to support them too.

  6. 2sn commented on Jan 15, 2022

    @2sn
    Author

    I just look up documentation for pcolorfast:
    https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.pcolorfast.html?highlight=pcolorfast#matplotlib.axes.Axes.pcolorfast
    where it states

    Carray-like
    The image data. Supported array shapes are:
    (M, N): an image with scalar data. The data is visualized using a colormap.
    (M, N, 3): an image with RGB values (0-1 float or 0-255 int).
    (M, N, 4): an image with RGBA values (0-1 float or 0-255 int), i.e. including transparency.
    

    So I think the ints not working is at least a deviation from the doc (if not a bug).

  7. 2sn commented on Jan 15, 2022

    @2sn
    Author

    (or my code has a bug - I had also tried with uint8, but as I mentioned, this fails because u was not listed as allowed dtype.kind.)

    Personally, I would also allow single int32 or int64 as color spec. I believe Pillow does, or at least as one of the options. It would be most efficient.

  8. added this to the v3.6.0 milestone on Jan 18, 2022
  9. tacaswell commented on Jan 18, 2022

    @tacaswell
    Member

    I just look up documentation for pcolorfast:

    That is a compelling arguement we should support [0,255] ints.


    The issue with supporting int32 or int64 is that currently we use the dimensionality to determine if we want to go through the color mapping process (for ndim=2) or directly color map (for ndim=3). The ambiguity there with the current imshow/pcolormesh API would be too much I think.

  10. 2sn commented on Jan 18, 2022

    @2sn
    Author

    Yes, I can see the degenerate cases you worry about. How to distinguish an N x 3 or N x 4 image in data type int from a (yet to be supported) byte 0-255 1D color image? Should be rare on the inputs, but what I have seen from the deeper down guts of current matplotlib, I can see where thing can get more ambiguous down the line. There could be a keyword.

  11. modified the milestones: v3.6.0, v3.7.0 on Aug 24, 2022
  12. greglucas commented on Dec 4, 2022

    @greglucas
    Contributor

    I just pushed a commit to #24619 that fixes this case for pcolorfast as well, although note that you need uint8 for integer 0-255 values in images.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions