How to create a custom colormap ("seismic" from python)

37 vues (au cours des 30 derniers jours)
Alfredo Scigliani
Alfredo Scigliani le 21 Mar 2023
Modifié(e) : DGM le 8 Mar 2025
I have read the colormap helpdocs but I am in need of a bit of help. I am not so familiar with RGB codes for colors, but I am trying to create a colormap like the one attached. It goes from red to white in the middle and then from white to blue towards the bottom end. How can I achieve something like this?
This specific colormap is called "seismic" in python.

Réponse acceptée

KSSV
KSSV le 21 Mar 2023

Plus de réponses (2)

MJFcoNaN
MJFcoNaN le 21 Mar 2023
Hello,
The easiest way may be creating manually in "colormap editor" and then you can save it.
  1. Right-click the colorbar and select "colormap editor" (I'm not sure the exact English translate).
  2. You can add/delete/change those key-colors, and Matlab will do the interpolation.
  3. You may choose the default "jet" colormap as a template. then delete some unwanted key-colors and add a white key-color in the middle.
  1 commentaire
Thomas
Thomas le 8 Mar 2025
This was really easy and worked for me. I was also able to save it as a mat file and load it anytime I want now. Thank you!

Connectez-vous pour commenter.


DGM
DGM le 8 Mar 2025
Modifié(e) : DGM le 8 Mar 2025
Without relying on any external python dependencies, here is an adequately accurate approximation.
n = 256; % specify the length of the generated colortable
x0 = [0 0.25 0.5 0.75 1]; % these are the breakpoint locations (normalized)
CT0 = [0 0 0.3; 0 0 1; 1 1 1; 1 0 0; 0.5 0 0]; % the breakpoint colors
CT = interp1(x0,CT0,linspace(0,1,n)); % the generated color table
% use it for something
peaks(100); % a test plot
z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2)
colormap(CT)
shading flat
colorbar
clim([-8 8])
This isn't perfectly accurate, but it should be within about 1LSB (in uint8) for map lengths shorter than 512 -- assuming my source is correct. At the very least, it certainly feels a lot more elegant than having the breakpoints all slightly misaligned from such convenient numbers.
EDIT: Of course there are a bunch of different maps all called the same thing. Why did I bother assessing accuracy when it never mattered in the first place?
% this is also "seismic colormap from python"
n = 256; % specify the length of the generated colortable
x0 = [0 0.33 0.4 0.5 0.6 0.667 1];
CT0 = [0.6314 1 1; 0 0 0.749; 0.302 0.302 0.302; 0.8 0.8 0.8; 0.3804 0.2706 0; 0.749 0 0; 1 1 0];
CT = interp1(x0,CT0,linspace(0,1,n)); % the generated color table
% use it for something
peaks(100); % a test plot
z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2)
colormap(CT)
shading flat
colorbar
clim([-8 8])
Note that within this "seismic colormap" library, there are two relevant colormaps, one literally called "seismic", which has nothing to do with the given example, and one called "RWB", which is roughly similar to, but obviously not the same as the given example.
The first example was specifically taken from MPL, and is probably what's intended. Given the JPG damage and the number of negligibly unique reinventions of the same wheel, it's hard to be certain.

Catégories

En savoir plus sur Color and Styling dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by