Errorpatch

A patch of errors

Errorbars are one of the most common representations of standard deviations, standard errors and confidence intervals. Another representation is an errorpatch, as observed in the figure above. An example of such a graphical representation can be found in figure 2 of this research article. How to create such an errorpatch in Matlab is described here.

Data

Faking

To plot data, we first need the data. If you have your own data to plot, you can skip this section. Otherwise, please create some random data in Matlab:

%% Create fake data
X    = -90:10:90; % stimulus X
Nrep  = 50; % 50 repetitions
X    = repmat(X,1,Nrep); % apply repeats
Y    = 50*sind(1.2*X); % let's take a sinusoidal relationship between stimulus and response
Y    = Y+(abs(X)+20)/3.*randn(size(Y)); % add some Gaussian noise/variation (standard deviation depends on stimulus size)
 
 

The data will appear somewhat as in figure 1, that can be created in Matlab by:

figure(1)
h = plot(X,Y,'k.');
axis square
box off
xlabel('X');
ylabel('Y');
axis([-100 100 -100 100]);
set(gca,'XTick',-90:30:90,'YTick',-90:30:90);
image

 

Determine mean and standard deviation

Next, you should determine the mean 'response' for every unique 'stimulus', and the standard deviation. This can be achieved by:

 
%% Determine mean and std response for each stimulus
uX = unique(X); % unique values of X
nX = numel(uX); % number of unique X
 
mu = NaN(size(uX)); % initialization mean vector
sd = mu; % initialization std vector
se = mu; % also determine the standard error
for ii = 1:nX
  sel = X==uX(ii);
  mu(ii) = mean(Y(sel));
  sd(ii) = std(Y(sel));
  n = sum(sel); % number of responses
  se(ii) = sd(ii)./sqrt(n); % standard error
end
ci = 1.96*se; % 95% confidence interval
 

This mean data and the standard deviation can be easily represented with Matlab's errorbar function:

figure(2)
h = errorbar(uX,mu,sd,'ko-');
set(h,'MarkerFaceColor','w','LineWidth',2);
axis square
box off
xlabel('X');
ylabel('Y');
set(gca,'XTick',-90:30:90,'YTick',-90:30:90);
axis([-100 100 -100 100]);
image

Errorpatch

How can we visualize the data as an errorpatch? With Matlab's patch. The only tricky part is to make a data matrix that describes the data:

 
x           = [uX fliplr(uX)];
y           = [mu+sd fliplr(mu-sd)];

Making good use of Matlab's patch function and its figure and axis handles, we obtain figure 3.

 
figure(3)
hpatch = patch(x,y,'k'); % the errorpatch
set(hpatch,'EdgeColor','none'); % remove the edge
set(hpatch,'FaceColor',[.7 .7 .7]); % and change the 
hold on;
hline = plot(uX,mu,'k-'); % central line
set(hline,'LineWidth',2); % make nice thick line
set(hline,'Color','k'); % change color
axis square
box off
xlabel('X');
ylabel('Y');
set(gca,'XTick',-90:30:90,'YTick',-90:30:90);
 

The errorpatches have been colored light gray, and the edge colors have been removed. The mean is also plotted as a thick black line.

image

Instead of changing the 'facecolor' of the patch to make it appear lighter, you can also make the patch transparent by setting the patch's alpha.

 
hpatch = patch(x,y,'k'); % the errorpatch
set(hpatch,'EdgeColor','none'); % remove the edge
alpha(hpatch,0.4); % another option would be to use transparency
 

This creates a problem if you want to save this figure in a vector-format such as eps. By default, Matlab converts vector figures with transparancy / alpha to bitmaps. This can be circumvented by supplying the -painter option when saving the figure. However, Matlab then removes the transparency. In Adobe Illustrator, you can manually reinstate the transparency.

 
print('-depsc2','-painter','errorpatch'); % save eps-file with -painter option with filename errorpatch
 

PandA

With the PANDA function pa_errorpatch it is easy to create these errorpatches.