MATLAB | MATLAB配色不够用 全网最全的colormap补充包来啦

示例图片


前言

众所周知,MATLAB中的colormap只有少得可怜的几种:

有很多应用在很特殊的图形中的colormap几乎都没有,而每次写代码都要去找颜色图属实太麻烦,因此就有了开发集成包的想法,我之前出过一篇使用python全部配色的文章,但是代码写的比较飘导致老版本用不了,这次使用了比较基础的代码和调用方式,争取能让更多人能用上。

matplotlab颜色新增了一些,但这哪够,于是我将:

  • matplotlab
  • scicomap
  • cmasher
  • viscm

包全部集成了进来,终于有了这套包含200个colormap的工具函数slanCM


颜色展示

Perceptually Uniform Sequential 感知一致 colormap:

Pure Sequential 颜色较纯单方向渐变:

较复杂渐变:

Diverging 双方向渐变:

Cyclic 循环渐变(两侧颜色可以对接在一起):

Miscellaneous 混杂渐变色,用于一些山地、光谱等特殊图绘制:

Qualitative 离散colormap:


使用方法

不指定获取颜色个数会默认256色,举例获取[163]号彩虹色(rainbow),以下两种写法等价:

  • slanCM(‘rainbow’)
  • slanCM(163)

第二个参数可以指定获取颜色数量,例如获取30颜色:

  • slanCM(‘rainbow’,30)
  • slanCM(163,30)

将获取的颜色放入colormap函或者某些图像的CData即可,例如:

  • colormap(slanCM(‘rainbow’))

实际例子

demo1 曲面图

使用上述

  • colormap(slanCM(‘rainbow’))

进行颜色修改:

% 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号配色:

% demo2
XData=rand(15,15);
XData=XData+XData.';
H=fspecial('average',3);
XData=imfilter(XData,H,'replicate');

imagesc(XData)
% 使用slanCM的100号配色
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=':';

加个光照:

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 渐变柱状图

多试了几个颜色:

% 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 散点图

% 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)

两百组数据整理起来真真真真的巨累,希望大家该点赞的点赞,该在看的在看!!

未经允许本代码请勿作商业用途,引用的话可以引用我file exchange上的链接,可使用如下格式:

Zhaoxu Liu (2022). 200 colormap (https://www.mathworks.com/matlabcentral/fileexchange/120088-200-colormap), MATLAB Central File Exchange. 检索来源 2022/11/6.

若转载请保留以上file exchange链接及本文链接!!!!!

完整工具函数及示例:
更新时会跟进更新以下连接:
【链接】:https://pan.baidu.com/s/13-EfxRoP4A7HS_gunShNPg?pwd=slan
【提取码】:slan

  • 92
    点赞
  • 162
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
1 2/3维图像分割工具箱 2 PSORT粒子群优化工具箱 3 matlab计量工具箱Lesage 4 MatCont7p1 5 matlab模糊逻辑工具箱函数 6 医学图像处理工具箱 7 人工蜂群工具箱 8 MPT3安装包 9 drEEM toolbox 10 DOMFluor Toolbox v1.7 11 Matlab数学建模工具箱 12 马尔可夫决策过程(MDP)工具箱MDPtoolbox 13 国立SVM工具箱 14 模式识别与机器学习工具箱 15 ttsbox1.1语音合成工具箱 16 分数阶傅里叶变换的程序FRFT 17 魔方模拟器与规划求解 18 隐马尔可夫模型工具箱 HMM 19 图理论工具箱GrTheory 20 自由曲线拟合工具箱ezyfit 21 分形维数计算工具箱FracLab 2.2 22 For-Each 23 PlotPub 24 Sheffield大学最新遗传算法工具箱 25 Camera Calibration 像机标定工具箱 26 Qhull(二维三维三角分解、泰森图)凸包工具箱 2019版 27 jplv7 28 MatlabFns 29 张量工具箱Tensor Toolbox 30 海洋要素计算工具箱seawater 31 地图工具箱m_map 32 othercolor配色工具包 33 Matlab数学建模工具箱 34 元胞自动机 35 量子波函数演示工具箱 36 图像局域特征匹配工具箱 37 图像分割graphcut工具箱 38 NSGA-II工具箱 39 chinamap中国地图数据工具箱(大陆地区) 40 2D GaussFit高斯拟合工具箱 41 dijkstra最小成本路径算法 42 多维数据快速矩阵乘法 43 约束粒子群优化算法 44 脑MRI肿瘤的检测与分类 45 Matlab数值分析算法程序 46 matlab车牌识别完整程序 47 机器人工具箱robot-10.3.1 48 cvx凸优化处理工具箱 49 hctsa时间序列分析工具箱 50 神经科学工具箱Psychtoolbox-3-PTB 51 地震数据处理工具CREWES1990版 52 经济最优化工具箱CompEcon 53 基于约束的重构分析工具箱Cobratoolbox 54 Schwarz-Christoffel Toolbox 55 Gibbs-SeaWater (GSW)海洋学工具箱 56 光声仿真工具箱K-Wave-toolbox-1.2.1 57 语音处理工具箱Sap-Voicebox 58 贝叶斯网工具箱Bayes Net Toolbox(BNT) 59 计算机视觉工具箱VFfeat-0.9.21 60 全向相机校准工具箱OCamCalib_v3.0 61 心理物理学数据分析工具箱Palamedes1_10_3 62 生理学研究工具箱EEGLAB 63 磁共振成像处理工具箱CONN 18b 64 matlab 复杂网络工具箱 65 聚类分析工具箱FuzzyClusteringToolbox 66 遗传规划matlab工具箱 67 粒子群优化工具箱 68 数字图像处理工具箱DIPUM Toolbax V1.1.3 69 遗传算法工具箱 70 鱼群算法工具箱OptimizedAFSAr 71 蚁群算法工具箱 72 matlab优化工具箱 73 数据包络分析工具箱 74 图像分割质量评估工具包 75 相关向量机工具箱 76 音频处理工具箱 77 nurbs工具箱 78 Nurbs-surface工具箱 79 grabit数据提取工具箱 80 量子信息工具箱QLib 81 DYNAMO工具箱 82 NEDC循环的整车油耗量 83 PlotHub工具箱 84 MvCAT_Ver02.01 85 Regularization Tools Version 4.1 86 MatrixVB 4.5(含注册) 87 空间几何工具箱 matGeom-1.2.2 88 大数计算工具箱 VariablePrecisionIntegers 89 晶体织构分析工具包 mtex-5.7.0 90 Minimal Paths 2工具箱 91 Matlab数学建模工具箱

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

slandarer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值