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

69 次查看(过去 30 天)
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.

采纳的回答

KSSV
KSSV 2023-3-21

更多回答(2 个)

MJFcoNaN
MJFcoNaN 2023-3-21
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 个评论
Thomas
Thomas 2025-3-8,7:25
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!

请先登录,再进行评论。


DGM
DGM 2025-3-8,18:12
编辑:DGM 2025-3-8,19:07
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.

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by