How to display median and mean on the histogram but should not overlap if same?

5 views (last 30 days)
I am plotting some histograms but I have a small issue. If the mean and median are same then they overlap(which is perphas correct). But how can I display mean and median on the histogram but show them with arrows or dotted line. The code for plotting is as below. They are all barh plots. So the mean and median are on the Y axis.
function usummary(x,bins,names,units)
[p q]=hist(x,bins);
barh(q,p)
xlabel(units)
ylabel(names)
yy_tick=[min(q) mean(q) median(q) max(q)];
yy_label={num2str(min(x)),strcat('mean = ',num2str(mean(x))) , strcat('median = ',num2str(median(x))),num2str(max(x))};
y_tick=sort(yy_tick);
for i=1:length(y_tick)
j=find(y_tick(1,i)==yy_tick);
y_label(1,i)=yy_label(1,j(1,1));
end
set(gca,'YTick',y_tick)
set(gca,'YTickLabel',y_label)
end
Also,I am still tyring to figure out how can use the nargin function in this code. Thank you so much in advance.

Answers (1)

Image Analyst
Image Analyst on 17 Oct 2012
You can use text() to place any text anywhere you want. So set up your tick labels like you are doing now except without the mean, and then use text() to set up a label for the median just on the other side of the axis.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!