Insert lines in grouped bar plots
Show older comments
Dear all,
ive created some grouped bar charts in Matlab.
Here i have 3 groups of bars and i want to ad three lines which are concecting the top of the bars .
Just the green line is adjusted right, but violet and blue are adujsted to the middle of the bar set.
Any ideas for the code???

X = categorical({'1.0','2.0','4.0','5.28'});
A= [6700 13380 20060; 2250 4480 6700; 920 1810 2700;690 1360 2030]
%bar (A,A1, 'stacked')
a= bar (X,A)
T1 = [20060 6700 2700 2030]
T2 = [13380 4480 1810 1360]
T3 = [6700 2250 920 690 ]
plot (X,T1)
plot (X,T2)
plot (X,T3)

2 Comments
Rik
on 27 Aug 2020
The point of categorical is that the x-values don't mean anything. You probably need to create this plot with normal x-values if you want to adjust the location of the lines.
Even with the undocumented properties below you can't plot normally (at least on R2018a).
XOffset=get(a(n),'XOffset');if isempty(XOffset),XOffset=0;end
YOffset=get(a(n),'YOffset');if isempty(YOffset),YOffset=0;end
x_current=get(a(n),'XDataCache')+XOffset;
y_current=get(a(n),'YDataCache')+YOffset;
Matthias Stark
on 27 Aug 2020
Answers (1)
Good start but mixed metaphors...plot as numeric; use the categories as labels...
ADDENDUM:
Without creating the second array T, just use the YData property...
hBar=bar(A); % make bar plot
hold on
hL=arrayfun(@(i) plot(hBar(i).XData+hBar(i).XOffset,hBar(i).YData, ...
'Color', hBar(i).FaceColor, ...
'LineWidth', 2.5, ...
'Marker','o', ...
'MarkerFaceColor',hBar(i).FaceColor, ...
'MarkerEdgeColor','k'), [1:numel(hBar)]);
xticklabels(X) % use the categorical names for tick labels
5 Comments
Matthias Stark
on 27 Aug 2020
What release are you using? MATLAB finally exposed the EndPoints property beginning with R2019 I believe (I have 'b')
For earlier releases, use the "trick" Rik outlines above--
>> hBa(1).XOffset+hBa(1).XData
ans =
0.7778 1.7778 2.7778 3.7778
>> hBa(1).XEndPoints
ans =
0.7778 1.7778 2.7778 3.7778
>>
you see that the EndPoints values are those from XData location plus the hidden Offset. The anonymous function then becomes
hL=arrayfun(@(i) plot(hBa(i).XData+hBa(i).XOffset,T(i,:)),[1:numel(hBa)]);
Matthias Stark
on 28 Aug 2020
UPDATE: Just realized the problem you're seeing that confuses the issue that isn't the x locaton at all is that the three T arrays are numbered in reverse order; I built the T array asT=[T1;T2;T3]; on the presumption "one" would be first...and then never paid any attention to the height of the points as being the bar heights; just presumed a result of some other comparison as can plot the end height from the other data; don't need a separate array.
So, rearranging so
T=flipud([T1;T2;T3]);
then one gets

with
hL=arrayfun(@(i) plot(hBar(i).XData+hBar(i).XOffset,T(i,:), ...
'Color', hBar(i).FaceColor, ...
'LineWidth', 2.5, ...
'Marker','o', ...
'MarkerFaceColor',hBar(i).FaceColor, ...
'MarkerEdgeColor','k'), [1:numel(hBar)]);
dpb
on 29 Aug 2020
hL=arrayfun(@(i) plot(hBar(i).XData+hBar(i).XOffset,hBar(i).YData, ...
'Color', hBar(i).FaceColor, ...
'LineWidth', 2.5, ...
'Marker','o', ...
'MarkerFaceColor',hBar(i).FaceColor, ...
'MarkerEdgeColor','k'), [1:numel(hBar)]);
is the simpler way -- don't need array T at all...
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!