Skip to content

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

Closed
@2sn

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

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