MATLAB Answers

Insert lines in grouped bar plots

조회 수: 5(최근 30일)
Matthias Stark
Matthias Stark 27 Aug 2020
편집: dpb 29 Aug 2020
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

Rik
Rik 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
Matthias Stark 27 Aug 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.

댓글을 달려면 로그인하십시오.

답변(1개)

dpb
dpb 27 Aug 2020
편집: dpb 29 Aug 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

표시 이전 댓글 수: 2
Matthias Stark
Matthias Stark 28 Aug 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 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
dpb 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...

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by