MATLAB is a great software tool for doing quick mathematical and statistical analyses. The power of MATLAB lies in its enormous (and constantly expanding) set of build in libraries and function. In addition, MATLAB provides a large number of plotting and visualization tools. However, once upon a time (when I was trying to make Figure 6 for this PLoS Biology publication), I needed to make a 3D scatterplot with errorbars and to my surprise there was no built in MATLAB function for creating such a plot. I tried gnuplot, R, and a general internet search, and I didn’t find anything that seemed suitable and affordable. So I rolled my own and this is what I came up with:

```
function [h]=plot3d_errorbars(x, y, z, e)
% this matlab function plots 3d data using the plot3 function
% it adds vertical errorbars to each point symmetric around z
% I experimented a little with creating the standard horizontal hash
% tops the error bars in a 2d plot, but it creates a mess when you
% rotate the plot
%
% x = xaxis, y = yaxis, z = zaxis, e = error value
% create the standard 3d scatterplot
hold off;
h=plot3(x, y, z, '.k');
% looks better with large points
set(h, 'MarkerSize', 25);
hold on
% now draw the vertical errorbar for each point
for i=1:length(x)
xV = [x(i); x(i)];
yV = [y(i); y(i)];
zMin = z(i) + e(i);
zMax = z(i) - e(i);
zV = [zMin, zMax];
% draw vertical error bar
h=plot3(xV, yV, zV, '-k');
set(h, 'LineWidth', 2);
end
```

file containing the function plot3_errorbars.m

file to create some sample data to try the function

The result of running the sample data through this function is show on the right. The errorbars will be different sizes in your data, because the error vector in the sample data above was randomly generated. We could just leave it the figure like this, however, 3d data are much easier to interpret/see if they have a surface fit to them.

So let’s add a few extra lines of code to fit a surface to our sample data

```
function [h]=plot3_errorbars_surf(x, y, z, e)
% this matlab function plots 3d data using the plot3 function
% it adds vertical errorbars to each point symmetric around z
% I experimented a little with creating the standard horizontal hash
% tops the error bars in a 2d plot, but it creates a mess when you
% rotate the plot
%
% x = xaxis, y = yaxis, z = zaxis, e = error value
% create the standard 3d scatterplot
hold off;
h=plot3(x, y, z, '.k');
% looks better with large points
set(h, 'MarkerSize', 25);
hold on
% now draw the vertical errorbar for each point
for i=1:length(x)
xV = [x(i); x(i)];
yV = [y(i); y(i)];
zMin = z(i) + e(i);
zMax = z(i) - e(i);
zV = [zMin, zMax];
% draw vertical error bar
h=plot3(xV, yV, zV, '-k');
set(h, 'LineWidth', 2);
end
% now we want to fit a surface to our data
% the 0.25 and 0.1 define the density of the fit surface
% adjust them to your liking
tt1=[floor(min(min(x))):0.25:max(max(x))];
tt2=[floor(min(min(y))):0.1:max(max(y))];
% prepare for fitting the surface
[xg,yg]=meshgrid(tt1,tt2);
% fit the surface to the data;
% matlab has several choices for the fit; below is "linear"
zg=griddata(x, y, z, xg,yg,'linear');
% draw the mesh on our plot
mesh(xg,yg,zg), xlabel('x axis'), ylabel('y axis'), zlabel('z axis')
```

file containing the function plot3_errorbars_surf.m

The result of running the sample data through this function is show on the right. As you can see, the 3d surface really aids the interpretation of the 3d data points. You also have to be careful though, because the type of surface you choose also biases the 3d interpretation, but that’s a different topic.

I hope these functions were useful to you. And good luck analyzing and visualizing your data.