Skip to content

[Bug]: LinearSegmentedColormap returns different results for int/float when used as a function #29487

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

Closed
JosephBARBIERDARNAL opened this issue Jan 20, 2025 · 1 comment

Comments

@JosephBARBIERDARNAL
Copy link
Contributor

JosephBARBIERDARNAL commented Jan 20, 2025

Bug summary

When invoking a LinearSegmentedColormap  object as a function, the output can differ based on whether you pass an integer or a float.

For example, in the code snippet below, cmap(1) returns a completely different result than cmap(1.0). While this behavior might be expected given how the colormap is implemented, it feels unintuitive.

IMO the provided reprex demonstrates the issue clearly, but please let me know if more details are needed.

Code for reproduction

from matplotlib.colors import LinearSegmentedColormap

cmap = LinearSegmentedColormap.from_list(name="reprex", colors=["red", "blue"])
print("cmap(0):", cmap(0))
print("cmap(1):", cmap(1))
print("cmap(1.0):", cmap(1.0))

Actual outcome

cmap(0): (np.float64(1.0), np.float64(0.0), np.float64(0.0), np.float64(1.0)) (red)
cmap(1): (np.float64(0.996078431372549), np.float64(0.0), np.float64(0.00392156862745098), np.float64(1.0)) (red)
cmap(1.0): (np.float64(0.0), np.float64(0.0), np.float64(1.0), np.float64(1.0)) (blue)

Expected outcome

cmap(0): (np.float64(1.0), np.float64(0.0), np.float64(0.0), np.float64(1.0)) (red)
cmap(1): (np.float64(0.0), np.float64(0.0), np.float64(1.0), np.float64(1.0)) (blue)
cmap(1.0): (np.float64(0.0), np.float64(0.0), np.float64(1.0), np.float64(1.0)) (blue)

Additional information

No response

Operating system

MacOS Sonoma 14.6.1

Matplotlib Version

3.10.0

Matplotlib Backend

module://positron_ipykernel.matplotlib_backend

Python version

Python 3.13.1

Jupyter version

/

Installation

pip

@timhoffm
Copy link
Member

This is a duplicate of #28198.

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

No branches or pull requests

2 participants