How can I compare two columns at the bar chart?

4 views (last 30 days)
I would like add this dotted line to the bar chart (MATLAB 2016a)

Accepted Answer

Adam Danz
Adam Danz on 19 Aug 2019
Here's a demo to get you started.
clf()
h = bar([210, 140, 175, 160]);
set(gca,'xtick',1:4,'xticklabel',{'A','B','C','D'})
hold on
% Get height of each bar
y = h.YData;
% draw horizontal line between B and D with a height
% equal to the tallest bar between them.
xLine = [2,4]; % draw line between bars centered at 2 and 4
halfWidth = 0.4; %estimate of 1/2 the width of the bars
maxHeight = max(y(xLine(1):xLine(2)));
plot(xLine + halfWidth*[-1,1], maxHeight * [1,1], ':','LineWidth',3, 'color', [1 0.64 0]) %orange
% draw horizontal line between A and D...
xLine = [1,4]; % draw line between bars centered at 1 and 4
maxHeight = max(y(xLine(1):xLine(2)));
plot(xLine + halfWidth*[-1,1], maxHeight * [1,1], 'g:','LineWidth',3)
% Draw vertical line at bar B, right edge
xLine = 2; % index of bar where vertical line will be drawn along right edge
plot(xLine+halfWidth * [1,1], [y(xLine), max(y)], 'r:','LineWidth',3)
190819 171411-Figure 1.jpg
  2 Comments
Erfan Zarei
Erfan Zarei on 19 Aug 2019
Thanks, very good
Do you think that this figure was drawn with another software?
Adam Danz
Adam Danz on 19 Aug 2019
Edited: Adam Danz on 21 Aug 2019
It's possible that another softward produced the figure but the figure can be replicated in Matlab, for sure. You can change the color of the bars to gray. You can also change the width of the bars. You can also make any needed changes to the axes, too.
Check out the properties in bar()
and axes()
You can use errorbar() to add the y-error
If you get stuck exploring those possibilities, let's continue the dialogue.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!