MATLAB Answers

Overlaying scatter plot on grouped bar graphs

19 views (last 30 days)
Hi All,
I am trying to plot a grouped bar graph. Along with the bars, I also need to plot single data points on each of the bars. Additionally, I would like to connect the individual data points with each other with a line. Here is the data I would like to use. Currently, I have been able to plot the bars and the error bars. But I am unsure about plotting the single data points. Is there a way to do that?
x = 1:3;
x = categorical({'High PU-Low RU', 'Low PU-High RU', 'Low PU-Low RU'});
SEM = [0.092290761 0.08898688 0.086731951 0.0825611
0.07279865 0.077586879 0.075656197 0.076483
0.088012243 0.088217074 0.094249201 0.081997937]
y = [0.619105248 0.596942136 0.581815617 0.553836698
0.488348187 0.520468608 0.507517199 0.513063563
0.590404075 0.591778124 0.63224286 0.550058883]
all = [0.1 0.2 0.3
0.4 0.4 0.6
0.6 0.5 0.8
0.6 0.8 0.6
0.1 0.2 0.3
0.4 0.4 0.6
0.6 0.5 0.8
0.6 0.8 0.6
0.42 0.52 0.62
0.72 0.72 0.92
0.92 0.82 1.12
0.92 1.12 0.92
0.42 0.52 0.62
0.72 0.72 0.92
0.92 0.82 1.12
0.92 1.12 0.92
]

Accepted Answer

Star Strider
Star Strider on 20 Jul 2021
The ‘all’ matrix is (16x3), however there are 12 bars (3 groups of 4). Ths size of ‘all’ is not consistent with that.
How is the ‘all’ matrix to be plotted with respect to the bars? I have chosen one option here. Clarify if I guessed incorrectly.
x = 1:3;
xlbl = {'High PU-Low RU', 'Low PU-High RU', 'Low PU-Low RU'};
SEM = [0.092290761 0.08898688 0.086731951 0.0825611
0.07279865 0.077586879 0.075656197 0.076483
0.088012243 0.088217074 0.094249201 0.081997937];
y = [0.619105248 0.596942136 0.581815617 0.553836698
0.488348187 0.520468608 0.507517199 0.513063563
0.590404075 0.591778124 0.63224286 0.550058883];
all = [0.1 0.2 0.3
0.4 0.4 0.6
0.6 0.5 0.8
0.6 0.8 0.6
0.1 0.2 0.3
0.4 0.4 0.6
0.6 0.5 0.8
0.6 0.8 0.6
0.42 0.52 0.62
0.72 0.72 0.92
0.92 0.82 1.12
0.92 1.12 0.92
0.42 0.52 0.62
0.72 0.72 0.92
0.92 0.82 1.12
0.92 1.12 0.92];
figure
hBar = bar(x, y.'); % Return ,bar Handle
for k1 = 1:size(y,2)
ctr(k1,:) = bsxfun(@plus, hBar(k1).XData, hBar(k1).XOffset'); % Note: ;XOffset; Is An Undocumented Feature, This Selects The ‘bar’ Centres
ydt(k1,:) = hBar(k1).YData; % Individual Bar Heights
end
hold on
errorbar(ctr, ydt, SEM.', '.g', 'MarkerSize',1)
plot(x, all, '.-', 'LineWidth',1.5)
xticklabels(xlbl);
.
  3 Comments
Prashanti Ganesh
Prashanti Ganesh on 21 Jul 2021
The reason for the inconsistency in the dimensions of all is because there are many data points whose avg is represented by one bar. For instance, in the following figure each bar represents the avg of the single data points overlaid on the bars. In this case, y was a matrix of 1 by 4 while all was a matrix of 50 by 4. I just want the same thing, for each of the bars, in a grouped bar graph. Even I am unsure what the dimensions of all should be like.

Sign in to comment.

More Answers (1)

Faraz Hedayati
Faraz Hedayati on 20 Jul 2021
Edited: Faraz Hedayati on 20 Jul 2021
If I understand your question, you want to plot a few more points. just use plot(x,y,'o')
i added one point above the first bar in yellow.
  2 Comments
Prashanti Ganesh
Prashanti Ganesh on 20 Jul 2021
Basically, I want to plot the 'all' data as scatter points on the bars.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by