Skip to content

Commit

Permalink
target named axis
Browse files Browse the repository at this point in the history
  • Loading branch information
raacampbell committed Nov 15, 2022
1 parent 263537a commit 80c07a5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ set(gca,'XTickLabel',[],'YTickLabel',[])


# Change Log
15tth November 2022
* Allow targing to a specific axis

8th Novemeber 2019
* Merge Octave support from [JarlPed](https://github.com/JarlPed/)
Expand Down
79 changes: 50 additions & 29 deletions shadedErrorBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
% 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.
% 'plotAxes' - handle of axis to plot into. By default this is gca
%
%
%
Expand All @@ -42,15 +43,19 @@
% 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
Expand All @@ -63,6 +68,13 @@
% hold off
%
%
% 5) Target to a specific axis
% clf
% a = subplot(1,2,1);
% subplot(1,2,2);
% shadedErrorBar(x,mean(y,1),std(y),'lineProps','g','plotAxes',a);
%
%
% Rob Campbell - November 2009


Expand All @@ -81,13 +93,15 @@
params.addParameter('transparent', false, @(x) islogical(x) || x==0 || x==1);
end
params.addParameter('patchSaturation', 0.2, @(x) isnumeric(x) && x>=0 && x<=1);
params.addParameter('plotAxes', gca);

params.parse(varargin{:});

%Extract values from the inputParser
lineProps = params.Results.lineProps;
transparent = params.Results.transparent;
patchSaturation = params.Results.patchSaturation;
plotAxes = params.Results.plotAxes;

if ~iscell(lineProps), lineProps={lineProps}; end

Expand Down Expand Up @@ -132,12 +146,18 @@


%Log the hold status so we don't change
initialHoldStatus=ishold;
if ~initialHoldStatus, hold on, end
initialHoldStatus = ishold(plotAxes);

H = makePlot(x,y,errBar,lineProps,transparent,patchSaturation);
if ~initialHoldStatus
cla(plotAxes,'reset') %Wipe the axes if we are not holding what was there before
hold(plotAxes,'on')
end

if ~initialHoldStatus, hold off, end
H = makePlot(x,y,errBar,lineProps,transparent,patchSaturation,plotAxes);

if ~initialHoldStatus
hold(plotAxes,'off')
end

if nargout==1
varargout{1}=H;
Expand All @@ -146,30 +166,30 @@



function H = makePlot(x,y,errBar,lineProps,transparent,patchSaturation)
function H = makePlot(x,y,errBar,lineProps,transparent,patchSaturation,plotAxes)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Determine host application
if (sum( size(ver('MATLAB'))) > 0 )
if sum( size(ver('MATLAB')) ) > 0
hostName = 'MATLAB';
elseif (sum(size(ver('Octave'))) > 0)
elseif sum( size(ver('Octave')) ) > 0
hostName = 'Octave';
end % if

% Plot to get the parameters of the line
if hostName == 'MATLAB'
H.mainLine=plot(x,y,lineProps{:});
H.mainLine=plot(plotAxes, x, y, lineProps{:});

elseif hostName == 'Octave'
boolxDatestr = checkOctave_datestr(x);
if boolxDatestr
x = datenum(x);
x = x(:).';
H.mainLine=plot(x,y,lineProps{:});
datetick(gca);
H.mainLine=plot(plotAxes, x, y, lineProps{:});
datetick(plotAxes);
else
H.mainLine=plot(x,y,lineProps{:});
H.mainLine=plot(plotAxes, x, y, lineProps{:});
end
end

Expand All @@ -185,45 +205,46 @@
edgeColor=mainLineColor+(1-mainLineColor)*0.55;

if transparent
faceAlpha=patchSaturation;
patchColor=mainLineColor;
faceAlpha = patchSaturation;
patchColor = mainLineColor;
else
faceAlpha=1;
patchColor=mainLineColor+(1-mainLineColor)*(1-patchSaturation);
faceAlpha = 1;
patchColor = mainLineColor + (1-mainLineColor) * (1-patchSaturation);
end


%Calculate the error bars
uE=y+errBar(1,:);
lE=y-errBar(2,:);
uE = y + errBar(1,:);
lE = y - errBar(2,:);


%Make the patch (the shaded error bar)
yP=[lE,fliplr(uE)];
xP=[x,fliplr(x)];
yP = [lE,fliplr(uE)];
xP = [x,fliplr(x)];

%remove nans otherwise patch won't work
xP(isnan(yP))=[];
yP(isnan(yP))=[];


if isdatetime(x) && strcmp(hostName,'MATLAB')
H.patch=patch(datenum(xP),yP,1);
H.patch = patch(datenum(xP),yP,1,'parent',plotAxes);
else
H.patch=patch(xP,yP,1);
H.patch = patch(xP,yP,1,'parent',plotAxes);
end


set(H.patch,'facecolor',patchColor, ...
'edgecolor','none', ...
'facealpha',faceAlpha, ...
'HandleVisibility', 'off', ...
'Tag', 'shadedErrorBar_patch')
set(H.patch, ...
'facecolor', patchColor, ...
'edgecolor', 'none', ...
'facealpha', faceAlpha, ...
'HandleVisibility', 'off', ...
'Tag', 'shadedErrorBar_patch')


%Make pretty edges around the patch.
H.edge(1)=plot(x,lE,'-');
H.edge(2)=plot(x,uE,'-');
H.edge(1) = plot(plotAxes, x, lE, '-');
H.edge(2) = plot(plotAxes, x, uE, '-');

set([H.edge], 'color',edgeColor, ...
'HandleVisibility','off', ...
Expand All @@ -232,7 +253,7 @@

% Ensure the main line of the plot is above the other plot elements
if hostName == 'MATLAB'
if strcmp(get(gca,'YAxisLocation'),'left') %Because re-ordering plot elements with yy plot is a disaster
if strcmp(get(plotAxes,'YAxisLocation'),'left') %Because re-ordering plot elements with yy plot is a disaster
uistack(H.mainLine,'top')
end
elseif hostName == 'Octave'
Expand Down

0 comments on commit 80c07a5

Please sign in to comment.