how to plot different horizontal lines over each group of bar plot
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Akzeptierte Antwort
Star Strider
am 8 Dez. 2023
Bearbeitet: Star Strider
am 8 Dez. 2023
Try something like this —
A = (5+rand(10))/6;
figure
hb = bar(A);
for k = 1:numel(hb)
xep(k,:) = hb(k).XEndPoints;
end
L = min(xep);
R = max(xep);
hold on
plot([L(:) R(:)], [1 1]*0.75, '-k', 'LineWidth',3)
hold off
figure
hb = bar(A);
for k = 1:numel(hb)
xep(k,:) = hb(k).XEndPoints;
end
L = min(xep);
R = max(xep);
hold on
plot([L(:) R(:)], [1 1]*0.75, '-k', 'LineWidth',3)
hold off
xlim([0.5 2.5])
Make appropriate changes to get the result you want.
EDIT — (8 Dec 2023 at 13:43)
To extend the lines to cover all the bars in each group (rather than stopping at the midpoints), use the normalised ‘Barwidth’ data as well to extend it —
A = (5+rand(10))/6;
figure
hb = bar(A);
BW = hb.BarWidth
for k = 1:numel(hb)
xep(k,:) = hb(k).XEndPoints;
end
L = min(xep);
R = max(xep);
hold on
plot([L(:) R(:)]+[-0.5 0.5]*BW/numel(hb), [1 1]*0.75, '-k', 'LineWidth',3)
hold off
figure
hb = bar(A);
BW = hb.BarWidth;
for k = 1:numel(hb)
xep(k,:) = hb(k).XEndPoints;
end
L = min(xep);
R = max(xep);
hold on
plot([L(:) R(:)]+[-0.5 0.5]*BW/numel(hb), [1 1]*0.75, '-k', 'LineWidth',3)
hold off
xlim([0.5 2.5])
.
2 Kommentare
Weitere Antworten (1)
Dyuman Joshi
am 8 Dez. 2023
Here's a demo -
%Random data
mat = randi([11 19], 4, 6);
%Bar plot
b = bar(mat);
hold on
%Get the x-coordinates of each group (center of each bar)
%Each column represents the x-coordinates of each bar group
x = vertcat(b.XEndPoints)
%Get the coordinates of the end points
x = x([1 end],:).'
plot(x, [5 5], 'LineWidth', 2.5, 'Color', 'k');
hold off
2 Kommentare
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!