MATLAB科研绘图进阶方案 | colorplot函数

图图本次提供全新的MATLAB绘图方案——基于MATLAB/plot定制的cplot函数,后续也将集成在FigureBestGUI中。

1. 一组样图

注:以上样式均经过“FigureBestGUI”软件的一键重调,原生cplot绘制出的图如下所示:

大家已经发现了,这个函数的功能是自动绘制出带涂色区域的线图,下一步我们来看看该函数是如何调用的!

2. cplot函数测试

2.1 一些简要说明

% 函数功能:将曲线与X轴围城的区域进行涂色% 函数重载规则(类似plot函数):% cplot(Y);% cplot(X,Y);% cplot(X,Y,S);% cplot(X1,Y1,X2,Y2,...);% cplot(X1,Y1,S1,X2,Y2,S2,...);% cplot(ax,__);% cplot(__,'Name','Value');% - 'FillColor' :指定颜色% - 'FillAlpha' :指定透明度

2.2 用了5张图作测试

x1 = 1:0.05:8;x2 = 0:0.05:7;%% 1figurecplot(x1);%% 2figurecplot(x2,sin(x2));%% 3figurecplot(x1,cos(x1),x2,sin(x2));%% 4figurecplot(x1,5-x1,'r',x2,sin(x2),'y');%% 5figurecplot(x1,5-x1,'r+',x2,sin(x2),'*y');

·第1、2、3个测试默认大家都已经明白·第4、5个测试单引号引起的部分代表样式值:r代表红色——+代表十字标记;

2.3 其它属性值修改方法

FillAlpha属性修改上色区不/透明度

cplot(x2,sin(x2),‘FillAlpha’,0.2);
cplot(x2,sin(x2),‘FillAlpha’,0.8);

FillColor属性修改上色区颜色

cplot(x2,sin(x2),‘FillColor’,[1 1 1]*0.6);

将图绘制在指定的坐标轴ax

cplot(ax,__);

3. cplot函数文件

建议:直接选择添加到路径

添加到路径成文内置函数方便以后直接调用
function h = cplot(varargin)% 函数功能:将曲线与X轴围城的区域进行涂色% 函数重载规则(类似plot函数):% cplot(Y);% cplot(X,Y);% cplot(X,Y,S);% cplot(X1,Y1,X2,Y2,...);% cplot(X1,Y1,S1,X2,Y2,S2,...);% cplot(ax,__);% cplot(__,'Name','Value');% - 'FillColor' :指定颜色% - 'FillAlpha' :指定透明度names = {'FillType','FillColor','FillAlpha'};fp = cell(1,3);for n = 1:length(names) for i = 1:length(varargin) if strcmpi(names{n},varargin{i}) if i+1 > nargin error(['Expected an input value after the name '' names{i} ''.']); end fp{n} = varargin{i+1}; varargin(i:i+1) = []; break; end endendif isscalar(varargin{1}) && ishandle(varargin{1}(1)) ax = varargin{1};else ax = gca;endtf = ishold(ax);ls = plot(varargin{:});hold(ax,'on');fd = fp{1};fc = fp{2};fa = fp{3};fd = validatetype(fd,ls);nf = size(fd,1);fc = validatecolor(fc,fd,ls,nf);fa = validatealpha(fa,nf);ps = gobjects(nf,1);for i = 1:nf x = cell(1,2); y = cell(1,2); for j = 1:2 switch fd(i,j) case {-2,-1} y{j} = ylim; y{j} = y{j}(abs(fd(i,j))); case 0 y{j} = 0; otherwise x{j} = ls(fd(i,j)).XData; y{j} = ls(fd(i,j)).YData; end end if isequal(x{1},x{2}) x = x{1}; elseif isempty(x{1}) x = x{2}; y{1} = y{1} * ones(size(x)); elseif isempty(x{2}) x = x{1}; y{2} = y{2} * ones(size(x)); else x = sort([x{1} x{2}]); y{1} = interp1(x{1},y{1},x,'linear','extrap'); y{2} = interp1(x{2},y{2},x,'linear','extrap'); end x0 = [x(1) zcross(x,y{1} - y{2}) x(end)]; y0 = interp1(x,y{1},x0); for j = 0:length(x0)-2 idx = x >= x0(j+1) & x <= x0(j+2) & y{1} >= y{2}; if all(~idx), continue; end xv = [x0(j+1) x(idx) x0(j+2) fliplr(x(idx))]; yv = [y0(j+1) y{1}(idx) y0(j+2) fliplr(y{2}(idx))]; ps(i) = fill(ax,xv,yv,fc(i,:),'LineStyle','none','FaceAlpha',fa(i)); endendif tf == 0 hold(ax,'off');endif nargout == 1 h = [ls;ps];endend
(0)

相关推荐