Matlab custom color map

Introduction

Matlab's own color map is relatively monotonous, and in many cases it cannot achieve the color map effect of other drawing tools, such as: NCL, Python matplotlib, GMT and other drawing software. Here's how to use the above-mentioned rich color map for your own use, and customize the color map.

method 1

Color map download

NCL official website provides a collection of color maps of the above drawing tools, so you only need to download and use the color maps you need.
Insert picture description here
Choose the color map WhiteBlueGreenYellowRed to download.

Matlab reads the downloaded color table

clc; clear all; close all

figure
mesh(peaks)

colorbar
color = ncl_colormap('WhiteBlueGreenYellowRed');
colormap(color)

figure
mesh(peaks)
num = 20;
color2 = color(floor(linspace(1,length(color),num)),:);
colormap(color2)
colorbar
function color = ncl_colormap(colorname)

temp = import_ascii([colorname '.rgb']);
temp(1:2) = [];
temp = split(temp,'#');
temp = temp(:,1);
% color = deblank(color);
temp = strtrim(temp);
temp = regexp(temp, '\s+', 'split');
for i=1:size(temp,1)
    color(i,:) = str2double(temp{
    
    i});    
end
color = color/255;
end
% 成行读取文本数据
% Edited Time:2019-02-22
function ascii = import_ascii(file_name)
i = 1;
fid = fopen(file_name);
while feof(fid) ~= 1
    tline = fgetl(fid);
    ascii{
    
    i,1} = tline; i = i + 1;
end
fclose(fid);
end

effect

Insert picture description here
Note: Figure 2 is a non-continuous color map, the scale and the color map do not correspond, you need to modify it yourself.

Method 2

To change the method, you need to download the rgbmap function and the rgb function.

h = surf(peaks);
colorbar
rgbmap('white','blue','green','yellow','red')
shading interp
set(h,'edgecolor','k','edgealpha',.2)
caxis([-5 5])
axis tight

Insert picture description here

to sum up

Of the two methods, the first method is relatively simple, and you can get a better color map without spending too much energy on color matching.

Guess you like

Origin blog.csdn.net/wokaowokaowokao12345/article/details/112969927