Insert lines in grouped bar plots

30 ビュー (過去 30 日間)
Matthias Stark
Matthias Stark 2020 年 8 月 27 日
編集済み: dpb 2020 年 8 月 29 日
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 2020 年 8 月 27 日
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 2020 年 8 月 27 日
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 2020 年 8 月 27 日
編集済み: dpb 2020 年 8 月 29 日
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 件のコメント
dpb
dpb 2020 年 8 月 28 日
編集済み: dpb 2020 年 8 月 29 日
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 2020 年 8 月 29 日
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...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by