MATLAB: How to plot multiple horizontal bar plots in single figure

barhplotting

Hi, I want to plot several bar plot or histograms on a line which are to represent the uncertainty of the data.
My problem is establishing the baseline for my bar plots. As I want them to be located different place, I need different baselines. But the basevalue option for my last bar plot seems to effect the basevalue of all the previous basevalues set.
The following example code below results in the figure to the left. While what I want is to get the right figure.
So how can I assign the Basevalue proberly to get the right figure?
% Some data
X = [1 100];
Y = [5 7.58];
A = normrnd(0,0.2,[1 1000])+7.5;
B = normrnd(1,0.2,[1 1000])+5;
[counts50,bins50] = hist(A,20);
[counts5,bins5] = hist(B,20);
figure()
hold on
plot(X,Y,'-'); % Plot of line
% Horizontal bar plot baseline 50
barh(bins50,50+counts50*0.5,'BarWidth',1,'Basevalue',50,'FaceColor','none','ShowBaseLine','off'); % Plot of histogram

% Horizontal bar plot basline 5
barh(bins5,5+counts5*0.05,'BarWidth',1,'Basevalue',5,'FaceColor','none','ShowBaseLine','off'); % Plot of histogram
% Options
set(gca,'FontSize',20,'XScale','log');
xticks([1 5 50 100]);
yticks=(5:8);
grid on
example.jpg

Best Answer

You'll have to do that by having two axes so that you can have two separate barh objects...I've railed at TMW for 20 years about how sorry their bar plots are in being so inflexible and difficult to work with.
That aside, you can begin with the venerable plotyy and then clean it up from there...
[hAx,hL,hR]=plotyy(bins5,5+counts5*0.5,bins50,50+counts50*0.5,@barh,@barh);
hAx(2).YLim=[5 8];
hAx(1).YLim=[5 8];
hAx(1).XLim=[1 125];
hAx(2).XLim=[1 125];
hAx(2).XTick=[];
hAx(2).YTick=[];
hAx(1).YTick=[5:8];
hAx(1).XScale='log';
hAx(2).XScale='log';
hAx(2).YLim=[5 8.5];
hAx(1).YLim=[5 8.5];
hAx(1).XTick=[1 5 50 100];
hR.BaseValue=50;
hL.BaseValue=5;
hR.ShowBaseLine='off';
hL.ShowBaseLine='off';
grid('on')
results in something close for starters...
And, you can be much more efficient in using set and handle arrays with array arguments; I just did this at command line on trial/error bais...
[hAx,hL,hR]=plotyy(bins5,c5,bins50,c50,@barh,@barh);
set(hAx,'YLim',[5 8.25]);
set(hAx,'XLim',[1 150]);
set(hAx,'XScale','log','XTick',[1 5 50 100],'YTick',[5:8]);
set(hAx(2),'XTick',[],'YTick',[]);
hR.BaseValue=50;
hL.BaseValue=5;
hB=[hL hR];
set(hB,'ShowBaseLine','off','BarWidth',1,'FaceColor','none');
set(hAx,'NextPlot','add')
hLn=plot(X,Y,'k-');
grid(hAx(1),'on')