Associating colors from a continuous colormap to specific values in matplotlib

marcos

I am trying to find a way to associate certain data values to specific colors in a continuous colormap.

I have a certain image with values ranging from [min, max], and I would like the following values [min, q1, q2, q3, max], where q'n' refers to the quartiles, to be associated to the colors that correspond to [0, 0.25. 0.5, 0.75. 1.0] in the colormap of choice. As a result the midpoint of the coloramp would correspond to the median value in the image, and so on...

I have been looking around, but I have not been able to find a way to do this.

Joe Kington

You'll need to subclass matplotlib.colors.Normalize and pass in an instance of your new norm to imshow/contourf/whatever plotting function you're using.

The basic idea is illustrated in the first option here: Shifted colorbar matplotlib (Not to shill one of my own questions too much, but I can't think of another example.)

However, that question deals specifically with setting a single data value to correspond to 0.5 in the colormap. It's not too hard to expand the idea to "piecewise" normalization, though:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize

class PiecewiseNormalize(Normalize):
    def __init__(self, xvalues, cvalues):
        self.xvalues = xvalues
        self.cvalues = cvalues

        Normalize.__init__(self)

    def __call__(self, value, clip=None):
        # I'm ignoring masked values and all kinds of edge cases to make a
        # simple example...
        if self.xvalues is not None:
            x, y = self.xvalues, self.cvalues
            return np.ma.masked_array(np.interp(value, x, y))
        else:
            return Normalize.__call__(self, value, clip)

data = np.random.random((10,10))
data = 10 * (data - 0.8)

fig, ax = plt.subplots()
norm = PiecewiseNormalize([-8, -1, 0, 1.5, 2], [0, 0.1, 0.5, 0.7, 1])
im = ax.imshow(data, norm=norm, cmap=plt.cm.seismic, interpolation='none')
fig.colorbar(im)
plt.show()

enter image description here

Note that 0.5 in the colormap (white) corresponds to a data value of 0, and the red and blue regions of the colormap are asymmetric (note the broad "pink" range vs the much narrower transition to dark blue).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Matplotlib: Custom colormap with three colors

From Dev

Exclude specific color/colors from colormap in groupby plot

From Dev

How to determine colors at intervals in a matplotlib colormap

From Dev

Plot histogram with colors taken from colormap

From Dev

How to recover key colors from a colormap?

From Dev

Map values to colors in matplotlib

From Dev

Applying a normalized scalar colormap to a matplotlib plot, still getting default colors

From Dev

Do matplotlib.contourf levels depend on the amount of colors in the colormap?

From Dev

Applying a normalized scalar colormap to a matplotlib plot, still getting default colors

From Dev

how to define colormap with absolute values with matplotlib

From Dev

Losing edge values in last colormap subplot matplotlib

From Dev

Assign Specific Colors to Specific Values

From Dev

How to create an interpolated colormap or color palette from two colors?

From Dev

Convert the standard colors from the old colormap to the new one

From Dev

How to set marker type for a specific point in a matplotlib scatter plot with colormap

From Dev

Setting discrete colormap corresponding to specific data range in Matplotlib

From Dev

Matplotlib: Use colormap AND use different markers for different values

From Dev

Associating values with keys in a HashMap from text file [Java]

From Dev

(C++) - Associating strings with values read in from a file

From Dev

How to set a colormap which can give me over 20 distinct colors in matplotlib?

From Dev

Create a colormap that is assigned to specific integer values with numpy/scipy

From Dev

Create a colormap that is assigned to specific integer values with numpy/scipy

From Dev

matplotlib heatmap with discrete values with custom defined colors

From Dev

Matplotlib and Pandas change colors of negative values

From Dev

Forcing `imshow` to properly color pixels based on `Colormap` colors when not all values are present in image?

From Dev

Set line colors according to colormap

From Dev

nonlinear colormap, matplotlib

From Dev

Colormap - Python / Matplotlib

From Dev

Matplotlib darker hsv colormap

Related Related

  1. 1

    Matplotlib: Custom colormap with three colors

  2. 2

    Exclude specific color/colors from colormap in groupby plot

  3. 3

    How to determine colors at intervals in a matplotlib colormap

  4. 4

    Plot histogram with colors taken from colormap

  5. 5

    How to recover key colors from a colormap?

  6. 6

    Map values to colors in matplotlib

  7. 7

    Applying a normalized scalar colormap to a matplotlib plot, still getting default colors

  8. 8

    Do matplotlib.contourf levels depend on the amount of colors in the colormap?

  9. 9

    Applying a normalized scalar colormap to a matplotlib plot, still getting default colors

  10. 10

    how to define colormap with absolute values with matplotlib

  11. 11

    Losing edge values in last colormap subplot matplotlib

  12. 12

    Assign Specific Colors to Specific Values

  13. 13

    How to create an interpolated colormap or color palette from two colors?

  14. 14

    Convert the standard colors from the old colormap to the new one

  15. 15

    How to set marker type for a specific point in a matplotlib scatter plot with colormap

  16. 16

    Setting discrete colormap corresponding to specific data range in Matplotlib

  17. 17

    Matplotlib: Use colormap AND use different markers for different values

  18. 18

    Associating values with keys in a HashMap from text file [Java]

  19. 19

    (C++) - Associating strings with values read in from a file

  20. 20

    How to set a colormap which can give me over 20 distinct colors in matplotlib?

  21. 21

    Create a colormap that is assigned to specific integer values with numpy/scipy

  22. 22

    Create a colormap that is assigned to specific integer values with numpy/scipy

  23. 23

    matplotlib heatmap with discrete values with custom defined colors

  24. 24

    Matplotlib and Pandas change colors of negative values

  25. 25

    Forcing `imshow` to properly color pixels based on `Colormap` colors when not all values are present in image?

  26. 26

    Set line colors according to colormap

  27. 27

    nonlinear colormap, matplotlib

  28. 28

    Colormap - Python / Matplotlib

  29. 29

    Matplotlib darker hsv colormap

HotTag

Archive