MATLAB: Two area plots with separate y axes and single log x-axes

areahistogramMATLAB

Hi folks.
Yesterday I had the need to overlay two histograms, with logarithmically scaled bin widths on a plot with a separate y-axis for each. The histogram needs to be filled in colour (as opposed to a simple line plot) and if possible semi-transparent. The combination of these requirements is causing problems.
Calculating the actual histogram data is no problem:
x1 = logspace(pow10LowerThanMin1,pow10HigherThanMax1,400);
n1 = hist(data1,x1);
x2 = logspace(pow10LowerThanMin2,pow10HigherThanMax2,400);
n2 = hist(data2,x2);
And plotting just the line plots for that is no problem:
[axes_handles,plot_handle1,plot_handle2]=plotyy(x1,n1,x2,n2);
set(axes_handles(1),'XScale','Log');
set(axes_handles(2),'XScale','Log');
So now I want to put in the fill areas. No problem, just use the axes handles as used above for the area function:
hold on;
area1_handle = area(axes_handles(1),x1,n1);
area2_handle = area(axes_handles(2),x2,n2);
The first area command works fine, plot comes out perfectly. The second area command returns with an error:
??? Invalid object handle
Error in ==> specgraph.areaseries.areaseries
at 19
h = specgraph.areaseries('parent',parent);
Error in ==> area at 85
h = [h
specgraph.areaseries('YData',datachk(y(:,k)),
...
It doesn't seem to be a problem with the axes handles themselves either, because if I try the second handle first it works, and the error occurs when calling area with axes_handles(2).
I've checked the axes_handles values against the axes handles returned by
get(gcf,'Children')
at each step and they are equal all the way through. After the error, only the first axis handle exists is returned by the above command.
So it appears that this is a bug of some sort. I've tried replacing the "area" function with a "fill" function and using "hist" with the "'histc'" option, but they all fail in similar fashion.
So my question is, does somebody have another way to generate my required plot?
Regards, Stephen

Best Answer

Set the NextPlot property of the AXES manually instead on hold('on'), because the later concerns the first axes only:
axesH = plotyy(1:5, rand(5), 6:7, rand(5));
set(axesH, 'XScale', 'log', 'NextPlot', 'add');
area(ax(1), 1:2, 0:1);
area(ax(2), 3:4, 0:1);
HOLD acts on the first axes only (even HOLD('all')):
axesH = plotyy(1:5, rand(5), 6:7, rand(5));
get(axesH, 'NextPlot') % >> 'replace', 'replace'
hold('on');
get(axesH, 'NextPlot') % >> 'add', 'replace'