MATLAB |
您所在的位置:网站首页 › matlab的优点有哪些 › MATLAB |
示例图片
众所周知,MATLAB中的colormap只有少得可怜的几种: 有很多应用在很特殊的图形中的colormap几乎都没有,而每次写代码都要去找颜色图属实太麻烦,因此就有了开发集成包的想法,我之前出过一篇使用python全部配色的文章,但是代码写的比较飘导致老版本用不了,这次使用了比较基础的代码和调用方式,争取能让更多人能用上。 matplotlab颜色新增了一些,但这哪够,于是我将: matplotlabscicomapcmasherviscm包全部集成了进来,终于有了这套包含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)使用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 rgbImage=imread("peppers.png"); imagesc(rgb2gray(rgbImage)) colormap(slanCM('prism2'))使用特殊地形配色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=':';加个光照: 多试了几个颜色: % 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=':';鉴于一部分人问过我咋从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 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |