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


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


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
hold off


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	
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

%X contains the corners of each bar so there are 4 values per bar. 
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

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


%Overlay the true mean (red) and the approximated mean (green)
hold on
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.