# MATLAB 绘图配色 **Repository Path**: yuanfeng1/slanColor ## Basic Information - **Project Name**: MATLAB 绘图配色 - **Description**: MATLAB 绘图配色 - **Primary Language**: Matlab - **License**: GPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 23 - **Created**: 2024-08-29 - **Last Updated**: 2024-08-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # MATLAB 绘图配色 ## 免登录下载链接 ### slanCM ![输入图片说明](slanCM/_demo5_2.png) 链接:https://pan.baidu.com/s/13-EfxRoP4A7HS_gunShNPg?pwd=slan \ 提取码:slan \ 若失效请用该链接:https://pan.baidu.com/s/1hObfj2bf5mjhGh5Vw9eKpQ?pwd=slan \ 提取码:slan 引用格式:Zhaoxu Liu / slandarer (2023). 200 colormap (https://www.mathworks.com/matlabcentral/fileexchange/120088-200-colormap), MATLAB Central File Exchange. 检索来源 2023/1/14. ### nclCM ![输入图片说明](nclCM/covor.png) 链接:https://pan.baidu.com/s/1s7WTRsQfUahufk6hVUuFRA?pwd=slan \ 提取码:slan \ 引用格式:Zhaoxu Liu / slandarer (2023). ncl colormaps (https://www.mathworks.com/matlabcentral/fileexchange/120848-ncl-colormaps), MATLAB Central File Exchange. 检索来源 2023/1/14. ### slanCL ![输入图片说明](slanCL/_1.gif) ![输入图片说明](slanCL/cover.png) 链接:https://pan.baidu.com/s/1oocjXDOQ_q1myB-1v29kgQ?pwd=slan \ 提取码:slan \ 引用格式:Zhaoxu Liu / slandarer (2023). 2000 palettes (https://www.mathworks.com/matlabcentral/fileexchange/126969-2000-palettes), MATLAB Central File Exchange. 检索来源 2023/3/28. ### scientific colormap 7 ![输入图片说明](ScientificColourMaps7/+ScientificColourMaps_FabioCrameri.png) 链接:https://pan.baidu.com/s/1rPgKAuhs7VQ4Fr73jMVmUw?pwd=slan \ 提取码:slan \ 引用格式:Crameri, Fabio. (2021). Scientific colour maps (7.0.1). Zenodo. https://doi.org/10.5281/zenodo.5501399 ## colormap中心点重置 + setPivot(n) + setPivot(ax,n) ```matlab imagesc(peaks(1000)+1) colormap([pink;flipud(bone)]) colorbar % 调整颜色图中点位置 setPivot(7) ``` ![输入图片说明](setPivot/1.png) ![输入图片说明](setPivot/2.png) ```matlab function setPivot(varargin) % @author:slandarer if nargin==0 ax=gca;pivot=0; else if isa(varargin{1},'matlab.graphics.axis.Axes') ax=varargin{1}; if nargin>1 pivot=varargin{2}; else pivot=0; end else ax=gca;pivot=varargin{1}; end end CLimit=get(ax,'CLim'); % CMap=get(ax,'Colormap'); CMap=colormap(ax); CLen=[pivot-CLimit(1),CLimit(2)-pivot]; if all(CLen>0) [CV,CInd]=sort(CLen); CRatio=round(CV(1)/CV(2).*300)./300; CRatioCell=split(rats(CRatio),'/'); if length(CRatioCell)>1 Ratio=[str2double(CRatioCell{1}),str2double(CRatioCell{2})]; Ratio=Ratio(CInd); N=size(CMap,1); CList1=CMap(1:floor(N/2),:); CList2=CMap((floor(N/2)+1):end,:); if mod(N,2)~=0 CList3=CList2(1,:);CList2(1,:)=[]; CInd1=kron((1:size(CList1,1))',ones(Ratio(1)*2,1)); CInd2=kron((1:size(CList2,1))',ones(Ratio(2)*2,1)); CMap=[CList1(CInd1,:);repmat(CList3,[Ratio(1)+Ratio(2),1]);CList2(CInd2,:)]; else CInd1=kron((1:size(CList1,1))',ones(Ratio(1),1)); CInd2=kron((1:size(CList2,1))',ones(Ratio(2),1)); CMap=[CList1(CInd1,:);CList2(CInd2,:)]; end % set(ax,'Colormap',CMap) colormap(ax,CMap); end end end ``` ## 非等据颜色分割 将任意比例点放置在任意数值处: ```matlab function setCMapRatio(varargin) % @author:slandarer if nargin==2 ax=gca; oriRatio=sort(varargin{1}); breakPnt=sort(varargin{2}); elseif nargin==3 ax=varargin{1}; oriRatio=sort(varargin{2}); breakPnt=sort(varargin{3}); end % 原始数据处理 CLimit=get(ax,'CLim'); breakPnt=[CLimit(1),breakPnt,CLimit(2)]; newRatio=diff(breakPnt); oriCMap=colormap(ax); CLen=size(oriCMap,1); newRatio=newRatio./diff([0,oriRatio,1]); newRatio=round(newRatio./max(newRatio).*400); oriRatio=[oriRatio,1]; % 最开始部分颜色条构造 tempCMap=oriCMap(1:ceil(oriRatio(1).*CLen),:); CInd2=kron((1:size(tempCMap,1)-1)',ones(newRatio(1),1)); newCMap=tempCMap(CInd2,:); CInd3=oriRatio(1).*CLen-size(tempCMap,1)+1; CInd3=round(CInd3.*newRatio(1)); newCMap=[newCMap;repmat(tempCMap(end,:),[CInd3,1])]; % 循环添加新的颜色 for i=2:length(oriRatio) CInd1=round(newRatio(i).*(ceil(oriRatio(i-1).*CLen)-oriRatio(i-1).*CLen)); if abs(ceil(oriRatio(i).*CLen)-oriRatio(i).*CLen)>0 CInd2=ceil(oriRatio(i-1).*CLen)+1:ceil(oriRatio(i).*CLen)-1; else CInd2=ceil(oriRatio(i-1).*CLen)+1:ceil(oriRatio(i).*CLen); end CInd2=kron(CInd2',ones(newRatio(i),1)); CInd3=round(newRatio(i).*(oriRatio(i).*CLen-floor(oriRatio(i).*CLen))); if ceil(oriRatio(i).*CLen)==ceil(oriRatio(i-1).*CLen) CInd1=[]; CInd3=round(newRatio(i).*(oriRatio(i).*CLen-oriRatio(i-1).*CLen)); end newCMap=[newCMap; repmat(oriCMap(ceil(oriRatio(i-1).*CLen),:),[CInd1,1]); oriCMap(CInd2,:); repmat(oriCMap(ceil(oriRatio(i).*CLen),:),[CInd3,1])]; end colormap(ax,newCMap); end ``` 非等据刻度值生成: ```matlab function cbTick=getCBTick(ax) % @author:slandarer if nargin<1,ax=gca;end CMap=colormap(ax); [~,Cind,~]=unique(CMap,'rows'); Cind=sort(Cind); Cind=Cind(2:end)-1; CLimit=get(ax,'CLim'); N=size(CMap,1); Cind=Cind./N; cbTick=[CLimit(1);CLimit(1)+(CLimit(2)-CLimit(1)).*Cind;CLimit(2)]; end ``` 实例: ```matlab contourf(rand(10,10)) colormap([pink(11);flipud(bone(11))]) cb=colorbar; setCMapRatio([8/22,10/22,20/22],[.2,.5,.8]) % 添加非等距离标签 cb.YTick=getCBTick(gca); % 调节colorbar刻度长度(这句代码可删掉) cb.TickLength=.061; ``` ![输入图片说明](%E4%B8%8D%E7%AD%89%E8%B7%9D%E9%A2%9C%E8%89%B2%E5%88%86%E5%89%B2/demo2_2.png) ## 自然配色 ![输入图片说明](%E8%87%AA%E7%84%B6%E9%85%8D%E8%89%B2/cover1.png) ![输入图片说明](%E8%87%AA%E7%84%B6%E9%85%8D%E8%89%B2/cover4.png) 链接:https://pan.baidu.com/s/1BzLWKmujs2tSGBEdeZaWVA?pwd=slan \ 提取码:slan