首页 > 其他分享 >matlab绘制阴影误差带函数

matlab绘制阴影误差带函数

时间:2023-05-08 12:12:58浏览次数:34  
标签:std 误差 bar errBar lineprops matlab shadedErrorBar 绘制 transparent

matlab误差带函数绘制效果

函数的input:(x,y,errBar,varargin)
%x:自变量x轴数据
%y:数据矩阵y
%errBar一般取y数据的均值和标准差,errBar可以是由两个函数句柄组成的cellArray。这个第一个定义了行应该是的统计信息,第二个定义了定义错误栏

调用语句shadedErrorBar(x, y, {@mean,@std}, 'lineprops', '-r');


function varargout=shadedErrorBar(x,y,errBar,varargin)
% generate continuous error bar area around a line plot
% function H=shadedErrorBar(x,y,errBar, ...)
% Purpose 
% Makes a 2-d line plot with a pretty shaded error bar made
% using patch. Error bar color is chosen automatically.
% Inputs (required)
% x - vector of x values [optional, can be left empty]
% y - vector of y values or a matrix of n observations by m cases
%     where m has length(x);
% errBar - if a vector we draw symmetric errorbars. If it has a size
%          of [2,length(x)] then we draw asymmetric error bars with
%          row 1 being the upper bar and row 2 being the lower bar
%          (with respect to y -- see demo). ** alternatively ** 
%          errBar can be a cellArray of two function handles. The 
%          first defines statistic the line should be and the second 
%          defines the error bar.
% Inputs (optional, param/value pairs)
% 'lineProps' - ['-k' by default] defines the properties of
%             the data line. e.g.:    
%             'or-', or {'-or','markerfacecolor',[1,0.2,0.2]}
% 'transparent' - [true  by default] if true, the shaded error
%               bar is made transparent. However, for a transparent
%               vector image you will need to save as PDF, not EPS,
%               and set the figure renderer to "painters". An EPS 
%               will only be transparent if you set the renderer 
%               to OpenGL, however this makes a raster image.
% 'patchSaturation'- [0.2 by default] The saturation of the patch color.
% Outputs
% H - a structure of handles to the generated plot objects.
% Examples:
% y=randn(30,80); 
% x=1:size(y,2);
% 1)
% shadedErrorBar(x,mean(y,1),std(y),'lineprops','g');
% 2)
% shadedErrorBar(x,y,{@median,@std},'lineprops',{'r-o','markerfacecolor','r'});
%
% 3)
% shadedErrorBar([],y,{@median,@(x) std(x)*1.96},'lineprops',{'r-o','markerfacecolor','k'});
% 4)
% Overlay two transparent lines:
% clf
% y=randn(30,80)*10; 
% x=(1:size(y,2))-40;
% shadedErrorBar(x,y,{@mean,@std},'lineprops','-r','transparent',1);
% hold on
% y=ones(30,1)*x; y=y+0.06*y.^2+randn(size(y))*10;
% shadedErrorBar(x,y,{@mean,@std},'lineprops','-b','transparent',1);
% hold off
%
%
% Rob Campbell - November 2009
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parse input arguments
narginchk(3,inf)
params = inputParser;
params.CaseSensitive = false;
params.addParameter('lineProps', '-k', @(x) ischar(x) | iscell(x));
params.addParameter('transparent', true, @(x) islogical(x) || x==0 || x==1);
params.addParameter('patchSaturation', 0.2, @(x) isnumeric(x) && x>=0 && x<=1);
params.parse(varargin{:});
%Extract values from the inputParser
lineProps =  params.Results.lineProps;
transparent =  params.Results.transparent;
patchSaturation = params.Results.patchSaturation;
if ~iscell(lineProps), lineProps={lineProps}; end
%Process y using function handles if needed to make the error bar dynamically
if iscell(errBar) 
    fun1=errBar{1};
    fun2=errBar{2};
    errBar=fun2(y);
    y=fun1(y);
else
    y=y(:).';
end
if isempty(x)
    x=1:length(y);
else
    x=x(:).';
end
%Make upper and lower error bars if only one was specified
if length(errBar)==length(errBar(:))
    errBar=repmat(errBar(:)',2,1);
else
    s=size(errBar);
    f=find(s==2);
    if isempty(f), error('errBar has the wrong size'), end
    if f==2, errBar=errBar'; end
end
if length(x) ~= length(errBar)
    error('length(x) must equal length(errBar)')
end
%Log the hold status so we don't change
initialHoldStatus=ishold;
if ~initialHoldStatus, hold on,  end
H = makePlot(x,y,errBar,lineProps,transparent,patchSaturation);
if ~initialHoldStatus, hold off, end
if nargout==1
    varargout{1}=H;
end
function H = makePlot(x,y,errBar,lineProps,transparent,patchSaturation)
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Plot to get the parameters of the line
    H.mainLine=plot(x,y,lineProps{:});
    % Work out the color of the shaded region and associated lines.
    % Here we have the option of choosing alpha or a de-saturated
    % solid colour for the patch surface.
    mainLineColor=get(H.mainLine,'color');
    edgeColor=mainLineColor+(1-mainLineColor)*0.55;
    if transparent
        faceAlpha=patchSaturation;
        patchColor=mainLineColor;
    else
        faceAlpha=1;
        patchColor=mainLineColor+(1-mainLineColor)*(1-patchSaturation);
    end
    %Calculate the error bars
    uE=y+errBar(1,:);
    lE=y-errBar(2,:);
    %Add the patch error bar
    %Make the patch
    yP=[lE,fliplr(uE)];
    xP=[x,fliplr(x)];
    %remove nans otherwise patch won't work
    xP(isnan(yP))=[];
    yP(isnan(yP))=[];
    if(isdatetime(x))
        H.patch=patch(datenum(xP),yP,1);
    else
        H.patch=patch(xP,yP,1);
    end
    set(H.patch,'facecolor',patchColor, ...
        'edgecolor','none', ...
        'facealpha',faceAlpha)
    %Make pretty edges around the patch. 
    H.edge(1)=plot(x,lE,'-','color',edgeColor);
    H.edge(2)=plot(x,uE,'-','color',edgeColor);
    uistack(H.mainLine,'top') % Bring the main line to the top

标签:std,误差,bar,errBar,lineprops,matlab,shadedErrorBar,绘制,transparent
From: https://www.cnblogs.com/Ada-CN/p/17381320.html

相关文章

  • MATLAB/Simulink直流微电网
    MATLAB/Simulink直流微电网子单元:风、光、储、负载、逆变器风力发电和光伏发电采用MPPT控制储能单元采用双环控制逆变器采用PQ控制可塑性高ID:15521666154133311......
  • MATLAB/Simulink
    MATLAB/Simulink交直流微电网孤岛风机,光伏,蓄电池MPPT控制、下垂控制、能量管理双向AC/DC,储能下垂ID:76800659111291208......
  • MATLAB/Simulink仿真,蓄电池SOC均衡
    MATLAB/Simulink仿真,蓄电池SOC均衡采用下垂控制,根据自身容量选择出力,直流母线电压、功率保持稳定无波动ID:58399657725318987......
  • 风电机组并网仿真模型MATLAB/Simulink
    风电机组并网仿真模型MATLAB/Simulink机侧:控制功率,功率外环电流内环网侧:控制直流侧电压和无功功率采用背靠背式功率变换器模型完整无错,运行稳定,适合研究直驱风机的工作原理等,可塑性高(风机输出功率、直流ID:86199657253152002......
  • MATLAB/Simulink仿真平台,蓄电池控制
    MATLAB/Simulink仿真平台,蓄电池控制包括蓄电池双向DC/DC控制,采用电压外环电流内环控制,使输出电压稳定,也可采用功率外环电流内环控制,使输出功率稳定ID:6550651179458582......
  • 500kV LCC-HVDC直流输电仿真模型Matlab
    500kVLCC-HVDC直流输电仿真模型Matlab采用十二脉波晶闸管换流阀,直流电流为2500A,整流侧采用直流电流PI控制,逆变侧采用直流电压PI控制,可以得到较好的2500A直流电流波形,直流电压在500kV动态平衡,可以得到交流侧多脉波波形,波形质量较好。ID:9640685196769041......
  • 两极三相光伏逆变并网仿matlab2021a
    两极三相光伏逆变并网仿matlab2021a采用mppt算法,扰动观察法,采用spwm调制,控制环采用双环PI调节,逆变器采用三相桥式逆变器,坐标变换含PLL锁相环,逆变器输出端加设LCL滤波器。无需发货,联系发邮件。ID:7720681036198445......
  • 三电平NPC逆变器矢量控制(SVPWM)matlab2021a
    三电平NPC逆变器矢量控制(SVPWM)matlab2021a采用矢量控制,大扇区、小扇区、矢量作用时间等均用程序编写,可以得到马鞍波调制波形逆变器输出三电平相电压波形,五电平线电压波形,经过滤波器后,可以得到对称的三相电压,电流ID:7440684413273739......
  • 电压型三相桥式逆变并网仿真Matlab2021
    电压型三相桥式逆变并网仿真Matlab2021电路采用两电平拓扑,采用双环PI控制,变换部分加设PLL锁相环,采用SPWM调制,逆变器输出端加设LCL滤波器,并入电网。可以得到逆变器输出端为三电平的线电压波形,滤波后可以得到对称三相电压、电流波形。无需发货,联系即可发邮件。ID:2217680461425092......
  • 三相桥式PWM整流电路matlab2021a
    三相桥式PWM整流电路matlab2021a整流电路采用全控型三相桥式整流电路,采用dq解耦控制,双环PI调节,采用SPWM调制,输入前加设LCL滤波器,可以得到整流后直流电压波形,波形质量较好。ID:7320680746184361......