MATLAB全網(wǎng)最全的colormap的使用教程詳解
示例圖片


前言
眾所周知,MATLAB中的colormap只有少得可憐的幾種:

有很多應(yīng)用在很特殊的圖形中的colormap幾乎都沒有,而每次寫代碼都要去找顏色的圖屬實(shí)太麻煩,因此就有了開發(fā)集成包的想法,我之前出過一篇使用python全部配色的文章,但是代碼寫的比較飄導(dǎo)致老版本用不了,這次使用了比較基礎(chǔ)的代碼和調(diào)用方式,爭(zhēng)取能讓更多人能用上。
matplotlab顏色新增了一些,但這哪夠,于是我將:
- matplotlab
- scicomap
- cmasher
- viscm
包全部集成了進(jìn)來,終于有了這套包含200個(gè)colormap的工具函數(shù)slanCM
顏色展示
Perceptually Uniform Sequential 感知一致 colormap:

Pure Sequential 顏色較純單方向漸變:

較復(fù)雜漸變:



Diverging 雙方向漸變:

Cyclic 循環(huán)漸變(兩側(cè)顏色可以對(duì)接在一起):

Miscellaneous 混雜漸變色,用于一些山地、光譜等特殊圖繪制:

Qualitative 離散colormap:

使用方法
不指定獲取顏色個(gè)數(shù)會(huì)默認(rèn)256色,舉例獲取[163]號(hào)彩虹色(rainbow),以下兩種寫法等價(jià):
- slanCM(‘rainbow’)
- slanCM(163)
第二個(gè)參數(shù)可以指定獲取顏色數(shù)量,例如獲取30顏色:
- slanCM(‘rainbow’,30)
- slanCM(163,30)
將獲取的顏色放入colormap函或者某些圖像的CData即可,例如:
colormap(slanCM(‘rainbow’))
實(shí)際例子
demo1 曲面圖
使用上述
colormap(slanCM(‘rainbow’))
進(jìn)行顏色修改:
% demo1
surf(peaks,'EdgeColor','w','EdgeAlpha',.3)
% 使用slanCM的彩虹配色
colormap(slanCM('rainbow'))
% 修飾一下
ax=gca;
ax.Projection='perspective';
ax.LineWidth=1.2;
ax.XMinorTick='on';
ax.YMinorTick='on';
ax.ZMinorTick='on';
ax.GridLineStyle=':';
view(-37,42)

demo2 imagesc
使用100號(hào)配色:
% demo2
XData=rand(15,15);
XData=XData+XData.';
H=fspecial('average',3);
XData=imfilter(XData,H,'replicate');
imagesc(XData)
% 使用slanCM的100號(hào)配色
colormap(slanCM(100))
hold on
ax=gca;
ax.DataAspectRatio=[1,1,1];
ax.LineWidth=1.2;
ax.XMinorTick='on';
ax.YMinorTick='on';
ax.ZMinorTick='on';
ax.GridLineStyle=':';
view(-37,42)

demo3 灰度圖
使用離散顏色:
% demo3
rgbImage=imread("peppers.png");
imagesc(rgb2gray(rgbImage))
colormap(slanCM('prism2'))

demo4 特殊地形配色
使用特殊地形配色terrain:
% demo4
X=linspace(0,1,200)';
CL=(-cos(X*2*pi)+1).^.2;
r=(X-.5)'.^2+(X-.5).^2;
surf(X,X',abs(ifftn(exp(7i*rand(200))./r.^.9)).*(CL*CL')*30,'EdgeColor','none')
colormap(slanCM('terrain'))
light
material dull
view(59.1823,56.1559)
% 修飾一下
ax=gca;
ax.Projection='perspective';
ax.LineWidth=.8;
ax.XMinorTick='on';
ax.YMinorTick='on';
ax.ZMinorTick='on';
ax.GridLineStyle=':';

加個(gè)光照:

demo5 多colormap
% demo5
X=linspace(0,1,200)';
CL=(-cos(X*2*pi)+1).^.2;
r=(X-.5)'.^2+(X-.5).^2;
Z=abs(ifftn(exp(7i*rand(200))./r.^.9)).*(CL*CL')*30;
ax1=axes('Parent',gcf,'OuterPosition',[0,1/2,1/2,1/2],'LooseInset',[0,0,0,0]);
contourf(Z,'EdgeColor','none')
ax1.Colormap=slanCM('tokyo',200);
ax2=axes('Parent',gcf,'OuterPosition',[1/2,1/2,1/2,1/2],'LooseInset',[0,0,0,0]);
contourf(Z,'EdgeColor','none')
ax2.Colormap=slanCM('sepia',200);
ax3=axes('Parent',gcf,'OuterPosition',[0,0,1/2,1/2],'LooseInset',[0,0,0,0]);
contourf(Z,'EdgeColor','none')
ax3.Colormap=slanCM('turku',200);
ax4=axes('Parent',gcf,'OuterPosition',[1/2,0,1/2,1/2],'LooseInset',[0,0,0,0]);
contourf(Z,'EdgeColor','none')
ax4.Colormap=slanCM('copper2',200);

demo6 帥氣的分形
% demo6
% MvLevi :https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/5/entries/10775
C=-9:9e-3:9;D=-9:9e-3:9;
for q=1:2001
for j=1:2001
X=.5;
for i=1:5
if mod(i,2)==0
X(i+1)=X(i)-C(q)*(.5+.3*cos(X(i)))^-1;
else
X(i+1)=X(i)-D(j)*(.5+.3*cos(X(i)))^-1;
end
end
P=diff(X);
L(q,j)=mean(log(abs(P)));
end
end
pcolor(C,D,-L)
shading flat
axis off
caxis([-3.5 3.5])
colormap(slanCM('twilight'))

demo7 漸變柱狀圖
多試了幾個(gè)顏色:
% demo7
X=randi([2,15],[1,25])+rand([1,25]);
b=bar(X);
CMap=slanCM('hsv');
b.FaceColor='flat';
b.CData=slanCM(188,length(b.XData));
% 42 56 63 100 133 187 188
% 修飾一下
ax=gca;hold on;grid on
ax.DataAspectRatio=[1,1,1];
ax.LineWidth=1.2;
ax.XMinorTick='on';
ax.YMinorTick='on';
ax.ZMinorTick='on';
ax.GridLineStyle=':';







demo8 散點(diǎn)圖
% demo8
rng('default')
for i = 1:20000
x = -0.4 + 0.8*randi([0 1],1,18);
A = gallery('circul',x);
E(:,i) = eig(A);
end
scatter(real(E(:)),imag(E(:)),8,'filled','CData',slanCM('twilight',length(E(:))))
xlabel('Re(E)')
ylabel('Im(E)')
xlim([-3 3])
ylim([-3 3])
axis square

demo9 氣泡圖
% demo9 x=1:30; [~,ind]=sort(rand(1,30)); x=x(ind); y=rand(1,30); sz=sort(rand(1,30)); % 100 102 94 bubblechart(x,y,sz,'CData',slanCM(94,30)); % 修飾一些 ax=gca;hold on ax.LineWidth=.8;



另(建議略過)
鑒于一部分人問過我咋從python獲取顏色,這里給兩段python代碼:
matplotlab獲取全部顏色:
import numpy as np
import matplotlib.pyplot as plt
cmaps = [('Perceptually Uniform Sequential', [
'viridis', 'plasma', 'inferno', 'magma', 'cividis']),
('Sequential', [
'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds', 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']),
('Sequential (2)', [
'binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink', 'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper']),
('Diverging', [
'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']),
('Cyclic', ['twilight', 'twilight_shifted', 'hsv']),
('Qualitative', [
'Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c']),
('Miscellaneous', [
'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', 'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'gist_rainbow', 'rainbow', 'jet', 'turbo', 'nipy_spectral', 'gist_ncar'])]
def plot_color_gradients(cmap_category, cmap_list):
print(cmap_category)
print(cmap_list)
for color in cmap_list:
np.savetxt(color+'.txt', np.c_[plt.get_cmap(color)(np.linspace(0, 1, 256))],fmt='%f',delimiter='\t')
for cmap_category, cmap_list in cmaps:
plot_color_gradients(cmap_category, cmap_list)
scicomap獲取全部顏色:
import numpy as np
import matplotlib.pyplot as plt
import scicomap as sc
import numpy as np
sc_map = sc.SciCoMap()
typeList=sc_map.get_ctype()
print(typeList)
sc_dic=sc.get_cmap_dict()
for i in typeList:
tdic=sc_dic[i]
print('-----------------')
for j in tdic.keys():
color=tdic[j]
np.savetxt(j+'.txt', np.c_[plt.get_cmap(color)(np.linspace(0, 1, 256))],fmt='%f',delimiter='\t')
print(j)
完
兩百組數(shù)據(jù)整理起來真真真真的巨累,希望大家該點(diǎn)贊的點(diǎn)贊,該在看的在看!!
file exchange:
Zhaoxu Liu (2022). 200 colormap (https://www.mathworks.com/matlabcentral/fileexchange/120088-200-colormap), MATLAB Central File Exchange. 檢索來源 2022/11/6.
以上就是MATLAB全網(wǎng)最全的colormap的使用教程詳解的詳細(xì)內(nèi)容,更多關(guān)于MATLAB colormap的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語(yǔ)言簡(jiǎn)明清晰講解結(jié)構(gòu)體
C語(yǔ)言結(jié)構(gòu)體(Struct)從本質(zhì)上講是一種自定義的數(shù)據(jù)類型,只不過這種數(shù)據(jù)類型比較復(fù)雜,是由 int、char、float 等基本類型組成的。你可以認(rèn)為結(jié)構(gòu)體是一種聚合類型2022-05-05
實(shí)現(xiàn)posix消息隊(duì)列示例分享
這篇文章主要介紹了實(shí)現(xiàn)posix消息隊(duì)列示例,學(xué)習(xí)記錄鎖,線程互斥量,線程條件變量,內(nèi)存映射,信號(hào),線程的綜合應(yīng)用,需要的朋友可以參考下2014-02-02
C++容器適配與棧的實(shí)現(xiàn)及dequeque和優(yōu)先級(jí)詳解
這篇文章主要介紹了C++容器適配與棧的實(shí)現(xiàn)及dequeque和優(yōu)先級(jí),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10
VS2017開發(fā)C語(yǔ)言出現(xiàn)“no_init_all“的解決辦法
這篇文章介紹了VS2017開發(fā)C語(yǔ)言出現(xiàn)“no_init_all“的解決辦法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12

