Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Qualitative colormaps re-map discrete color values #21786

Open
@jonahpearl

Description

@jonahpearl

Bug summary

When I started using qualitative color maps, I expected to be able to pass integers into the c argument of plt.scatter() like keys in a dictionary, basically. Integer in, color out, same relationship every time. Instead, the values get remapped based on min/max, same as quantitative colormaps. I think this is kind of silly: the whole point of a qualitative color map is that you can pick distinct values and pass them in to represent some kind of category, without worrying about the scale of the numbers. Indeed, this could even be misleading: if you pass in, say, color values (integers) ranging from 1-20 to the Set1 colormap, it will group some of them to be the same color, when imo it should raise an error and tell you that you picked a colormap that doesn't have enough colors.

I suspect I'm opening up a can of worms here, but a quick search didn't reveal any prior discussion, so figured I'd ask.

Also: I do realize that I can get the desired behavior by passing in actual rgb values for each point. But it took me a while to figure out why the heck the above strategy wasn't working, and I want to save everyone else that time!

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(3)
y = x
c = np.array([0,1,2])
plt.subplot(121)
plt.scatter(x,y,c=c, cmap='Set1')  # weird
plt.subplot(122)
plt.scatter(x,y,c=c, cmap='Set1', vmin=0, vmax=8) # the expected behavior

Actual outcome

image

Expected outcome

See above

Additional information

No response

Operating system

OSX

Matplotlib Version

3.4.3

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.9.7

Jupyter version

6.4.6

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions