MATLAB: How to plot a histogram and line on the same plot in MATLAB 7.11 (R2010b)

MATLAB

I would like to plot both a histogram and a line on the same plot. I have tried using the 'hold on' command, but the scales of the plots are quite different. I would like them to be plotted with separate scales.

Best Answer

It is possible to plot to lines with separate scales using the PLOTYY command. Once could get the handle to an axis generated by PLOTYY and replace the line plot with a histogram. After plotting the histogram, the y-axis can be rescaled as appropriate. the following code demonstrates this:
x1=1:10;
y1=10*rand(1,1000);
x=1:10;
y=rand(1,10);
axes=plotyy(x,y,x,y);
hold on
hist(axes(1),y1,x1);
ylim(axes(1),'auto');
set(axes(1),'ytickmode','auto');
hold off