How do I get values on top of my bar plot?

1 view (last 30 days)
I have data that I want to have shown in groups of three. How can I get the values Diff onto the bar plot? I have tried various ways but can't get it right...
Diff = [minD1 maxD1 meanD1; minD maxD meanD; minD2 maxD2 meanD2; minD3 maxD3 meanD3; minD4 maxD4 meanD4]*10^4;
figure
name = categorical({'100 ul', '200 ul', '500 ul', '750 ul', '1000 ul'});
name = reordercats(name,{'100 ul', '200 ul', '500 ul', '750 ul', '1000 ul'});
bar(name, Diff)
ylabel('Diffusion coefficent (10^-^4)')
legend('min D','max D','mean D')
grid on
grid minor
  4 Comments
darova
darova on 15 Jun 2020
Did you try text? Use for loop
Jenni
Jenni on 15 Jun 2020
I tried, but I could not get it to work... I got all text on each staple.

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 15 Jun 2020
See this example
% Diff = [minD1 maxD1 meanD1; minD maxD meanD; minD2 maxD2 meanD2; minD3 maxD3 meanD3; minD4 maxD4 meanD4]*10^4;
Diff = rand(5, 3);
name = {'100 ul', '200 ul', '500 ul', '750 ul', '1000 ul'};
figure;
ax = axes();
b = bar(1:5, Diff);
ylabel('Diffusion coefficent (10^-^4)')
legend('min D','max D','mean D')
grid on
grid minor
ax.XTickLabel = name;
barNames = {'a', 'b', 'c'};
x_diffs = [-0.25 -.05 0.2]; % x-location of bars relative to middle bar
y_diff = 0.02; % height of text above the bar
for i = 1:numel(b)
text(b(i).XData+x_diffs(i), b(i).YData+y_diff, barNames(i));
end
  4 Comments
Jenni
Jenni on 15 Jun 2020
Thank you very much! It works fine now!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!