Matlab自定义颜色图

本文介绍了如何在Matlab中使用NCL和Python的颜色图,提供两种方法:直接读取下载的颜色表应用到Matlab,以及通过下载rgbmap函数实现颜色映射。第一种方法简单快捷,第二种方法涉及更多自定义选项,如颜色连续性和刻度匹配。通过这些技巧,可以提升Matlab绘图的视觉效果。
摘要由CSDN通过智能技术生成

介绍

Matlab自带颜色图比较单调,很多时候无法达到其它绘图工具,如:NCL、Python matplotlib、GMT等绘图软件颜色图效果。下面就介绍如何将上述丰富的颜色图为己所用,以及自定义颜色图。

方法1

颜色图下载

NCL官网提供了上述绘图工具的颜色图合集,因此只需要将需要的颜色图下载后进行使用即可。
在这里插入图片描述
选择颜色图WhiteBlueGreenYellowRed下载。

Matlab读取下载的颜色表

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

效果

在这里插入图片描述
注意:图2为非连续颜色图,刻度和颜色图不对应,需要自己修改。

方法2

改方法需要下载rgbmap函数,以及rgb函数。

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

在这里插入图片描述

总结

两种方法,第一种方法比较简单,不用花费太多精力在颜色搭配上,就可以获得效果较好的颜色图。

  • 13
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值