How to plot multiple horizontal bar plots in single figure?

3 views (last 30 days)
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

Accepted Answer

dpb
dpb on 19 Mar 2019
Edited: dpb on 20 Mar 2019
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')
  3 Comments
Marius Hammer
Marius Hammer on 20 Mar 2019
Thanks, that did the job, altough a bit alternative solution using two axis.
I got it right using similar setup as from your first code. Your second code with set arguments didn't really work for me for some reason.
It's a bit sad that the Baseline property overrides previous baseline properties from regular plotting with one y-axis. Can't really see why that has to be.
dpb
dpb on 20 Mar 2019
Edited: dpb on 20 Mar 2019
Oh, shucks! That ended up being a slightly early version; there are two errors -- first and most important is need to set BOTH axes to log scale, not just the one. That oversight ends up tying both bar plots back together somehow that can't then get them to act totally independent again.
Secondly, I accidentally wiped out the left axes handle to the left barh object when used hL a second time for the line handle...I corrected both of those in the Answer.
I don't quite follow what is going on internally, either, that for some reason the two baselines are tied to the parent axes it seems, not the bar object itself. As I whined earlier, I've complained to TMW for over 20 years about how difficult and inflexible the BAR() plotting routines are to do even some of the most rudimentary obvious things and what a waste of users' time it is to have to figure out these workarounds/kludges. So far, the only changes have generally made things more, not less, difficult. :(

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!