Insert lines in grouped bar plots

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 commentaires

Rik
Rik le 27 Août 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
Matthias Stark le 27 Août 2020
Thanks for your answer,
The reason why i used categorial is that i need an even space between 2.0 and 4.0 as well as 5.28.
When i do this chart with x values, the spaces will be wrong.

Connectez-vous pour commenter.

Réponses (1)

dpb
dpb le 27 Août 2020
Modifié(e) : dpb le 29 Août 2020
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 commentaires

Matthias Stark
Matthias Stark le 27 Août 2020
Thanks for this answer,
But Matlab give me this output
No appropriate method, property, or field 'XEndPoint' for class 'matlab.graphics.chart.primitive.Bar'.
dpb
dpb le 27 Août 2020
Modifié(e) : dpb le 27 Août 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
Matthias Stark le 28 Août 2020
Iam using the R2018b, and your code is working now.
Many thanks, but theres just one problem:
Now each of the three lines are adusted to the middle bar auf each group. But i need to arrange
Plot 1 to Bar 1, Plot 2 to bar 2 and plot 3 to bar 3.
So, that every bar is conected with all bars in the same color.
Best regards.
dpb
dpb le 28 Août 2020
Modifié(e) : dpb le 29 Août 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
dpb le 29 Août 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...

Connectez-vous pour commenter.

Tags

Modifié(e) :

dpb
le 29 Août 2020

Community Treasure Hunt

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

Start Hunting!

Translated by