recipes : Plotting : Plot Manipulation : Overlay mean on a histogram

Problem

How do I overlay the mean of a distribution onto a histogram?

Solution

First calculate the mean of your distribution then choose a way to plot this over the histogram. One option is shown below.

R=randn(1,100); %100 random draws from the normal distribution 
hist(R,20); %Plot data divided into 20 bins

mu=mean(R); %The mean

%Overlay the mean
hold on
plot([mu,mu],ylim,'r--','LineWidth',2)
hold off

%Discussion:
set(gca,'YLimMode','Manual')
Discussion

Ok, so that was pretty simple but what if you've plotted your data, not overlaid he mean, then lost the variable used to make plot? Luckily there is a solution! All of the data displayed in a plot is always stored within an axe's properties. Here we see how to extract this and use it to overlay the mean.

%Generate data as before	
R=randn(100,1);
mu=mean(R); %store the true mean for reference

hist(R,20) %plot the histogram

%Use the handle of the plot's patch object to extract the data
H=findobj('Type','Patch');

%X contains the corners of each bar so there are 4 values per bar. 
X=get(H,'XData'); 
X=mean(X,1); %The mean of the 4 approximates is the average X value for each bar 

%The second row of the Y data is the height of the bar
Y=get(H,'YData');
Y=Y(2,:);

%We now try to approximate the original data by expanding each X value 
%by the height of its corresponding bar
OUT=[];
for ii=1:length(Y)
    OUT=[OUT,repmat(X(ii),1,Y(ii))];
end

muHat=mean(OUT);

%Overlay the true mean (red) and the approximated mean (green)
hold on
plot([mu,mu],ylim,'r--','linewidth',2)
plot([muHat,muHat],ylim,'g--','linewidth',2)
hold off

As you can see, we had to approximate the mean in this case. We had to approximate because the data that were plotted were not the raw data (as would have been the case if this were a scatter plot, say) but binned data. In most other plot types you will be able to extract the raw data. Nevertheless, our calculated mean (green) is pretty close and certainly good enough for display purposes. It's useful to know that this is possible, even though it's best not to have to resort to this method of finding the mean.