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.
You'll need to subclass
matplotlib.colors.Normalize
and pass in an instance of your newnorm
toimshow
/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:
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).