以前Matlab作图时,要设置好多的变量, 如标题,x轴,y轴间隔, 字体,线条等等特性的设置。这些在写的时候比较麻烦, 今天写了一个集成的函数,直接包含了一个基本的特性,只要用结构体包含进来就可以了。非常的方便。
例子1
代码
% Demo shows how to:
% 1. use cell arrays when lines have different length
% 2. use repeating sequence of markers/colors/lineStyles
% 3. truncate axes to partially-specified limits
clear all
close all
%% Generate some data
maxIter = 200;
optTol = 1e-6;
err0 = 10000;
rho = .04;
rho2 = .04:.005:1;
for k = 1:maxIter
err1(k) = err0/sqrt(k);
if err1(k) < optTol
break;
end
end
for k = 1:maxIter
err2(k) = err0/k;
if err2(k) < optTol
break;
end
end
for k = 1:maxIter
err3(k) = err0/k^2;
if err3(k) < optTol
break;
end
end
for k = 1:maxIter
err4(k) = err0*(1-rho)^k;
if err4(k) < optTol
break;
end
end
for k = 1:maxIter
err5(k) = err0*(1-sqrt(rho))^k;
if err5(k) < optTol
break;
end
end
for k = 1:maxIter
err6(k) = err0*prod(1-rho2(1:k));
if err6(k) < optTol
break;
end
end
% Note: yData does not have same length, and is stored as cell array
xData = 1:maxIter;
yData{1} = err1;
yData{2} = err2;
yData{3} = err3;
yData{4} = err4;
yData{5} = err5;
yData{6} = err6;
legendStr = {'Sublinear1','Sublinear2','Sublinear3','Linear1','Linear2','Superlinear'};
%% Pretty Plot
figure;
options.logScale = 2;
% Note: prettyPlot will cycle through the given colors/lineStyles/markers
options.colors = [1 0 0
0 1 0
0 0 1];
options.lineStyles = {':','--','-'};
options.markers = {'o','s'};
% Note: we will truncate the lower y-axis limit but not the upper limit
options.ylimits = [1e-6 inf];
options.markerSpacing = [25 1
25 11
25 21
25 5
25 15
25 8];
options.xlabel = 'Iteration Number'; %% 设置的坐标
options.ylabel = 'Error';
options.legend = legendStr; %% 设置Graph legend
options.legendLoc = 'SouthWest'; %% 位置
%% 另外自己也可以写好多的函数。。。。。。很方便。
prettyPlot(xData,yData,options);
print -dpdf prettyPlot2.pdf
效果图:
关于prettyplot函数:
function [] = prettyPlot(xData,yData,options)
% prettyPlot(xData,yData,options)
%
% available options:
% legend - cell array containing legend entries (default = [])
% title - string containing plot title (default = [])
% xlabel - string containing x-axis label (default = [])
% ylabel - string containing y-axis label (default = [])
% lineWidth - width of lines (default = 3)
% colors - cell array or (n by 3) matrix containing line colors (default = 'b')
% lineStyles - cell array containing line styles (default = '-')
% markerSize - size of markers (default = [])
% markers - cell array containing markers (default = [])
% markerSpacing - (n by 2) matrix containing spacing between markers and offset for first marker
% xlimits - 2-vector containing lower and upper limits for x-axis (can be inf to show full range)
% ylimits - 2-vector containing lower and upper limits for y-axis (can be inf to show full range)
% logScale - can be 0 (regular scale), 1 (semilogx), or 2 (semilogy)
% legendLoc - location of legend (default = 'Best')
% useLines - whether to use lines (default = 1)
% fillFace - whether to fill markers (default = 1)
% errors - (n by p by 2) array containing upper and lower error lines
% errorStyle - line style for error bars
if nargin < 3
options = [];
end
[legendStr,plotTitle,plotXlabel,plotYlabel,lineWidth,colors,lineStyles,...
markerSize,markers,markerSpacing,xlimits,ylimits,logScale,legendLoc,...
useLines,fillFace,errors,errorStyle,errorColors] = ...
myProcessOptions(options,'legend',[],'title',[],'xlabel',[],'ylabel',[],...
'lineWidth',3,'colors',[],'lineStyles',[],...
'markerSize',12,'markers',[],'markerSpacing',[],...
'xlimits',[],'ylimits',[],...
'logScale',0,'legendLoc','Best','useLines',1,'fillFace',1,...
'errors',[],'errorStyle',{'--'},'errorColors',[]);
if logScale == 1
plotFunc = @semilogx;
elseif logScale == 2
plotFunc = @semilogy;
else
plotFunc = @plot;
end
if useLines == 0
defaultStyle = 'b.';
else
defaultStyle = 'b';
end
if iscell(yData)
nLines = length(yData);
else
nLines = size(yData,1);
end
for i = 1:nLines
% Get yData for line
if iscell(yData)
y{i} = yData{i};
else
y{i} = yData(i,:);
end
% Get xData for line