How do I add text detailing the bar's value to the top of each bar graph to 3 significant figures
Show older comments
I am trying to create a bar graph to display some energy consumption data, calculated from using the 'trapz' function. I have successfully produced a graph however, I have been unable to add text to the top of each bar. I have looekd at online examples but I think because my energy data is a 4x3 double, I need to produce some sort of loop that moves through each bar individually. Does anyoen know how to do this, I would really appreciate it if someone sent me an example code as I have tried everything and have not managed to do this.
Here is my current code;
%% Plot Light Normal Bar Graph of Energy Consumption based off Power Curve
figure;
weights_normal_1 = [1 2 5];
energy_normal_1 = [TwoJPG_energy_normal_1; TwoFG_energy_normal_1; ThreeFG_energy_normal_1; FourFG_energy_normal_1];
b4 = bar(categorical(weights_normal_1), energy_normal_1', 'grouped');
b4(3).FaceColor = [0.5 0 0.5];
b4(4).FaceColor = [0 0.6 0];
grid on;
grid minor;
% Add legend and axis labels
legend({'Two Jaw Parallel Gripper', 'Two Finger Gripper', 'Three Finger Gripper', 'Four Finger Grippper'}, 'Location', 'northwest');
xlabel('Weight (kg)');
ylabel('Energy Consumed (J)');
title('Energy Consumption in the Light Normal Working Range');
Where the energy data is of the form;
energy_normal_1 = [(497.996651896180, 997.446068680039, 2497.85395323365);
(200.324767943332, 400.925364590215, 1003.40714370146);
(100.162383971666, 200.462682295108, 1254.25892962683);
(100.182131540439, 200.364544680224, 501.284667218855)]
I have attcahed the graph I produced.
Just to summarise, I would like each energy value placed as a text on top of each bar in the bar graph, e.g. 497.9 above first bar.
Thanks in advance for your help.
Accepted Answer
More Answers (1)
Hello Archie,
But something like this should do the trick.
Another option would be to loop through the bar and use the command XEndPoints and YEndPoints.
%% Plot Light Normal Bar Graph of Energy Consumption based off Power Curve
figure;
weights_normal_1 = [1 2 5];
% energy_normal_1 = [TwoJPG_energy_normal_1; TwoFG_energy_normal_1; ThreeFG_energy_normal_1; FourFG_energy_normal_1];
energy_normal_1 = [497.996651896180, 997.446068680039, 2497.85395323365;
200.324767943332, 400.925364590215, 1003.40714370146;
100.162383971666, 200.462682295108, 1254.25892962683;
100.182131540439, 200.364544680224, 501.284667218855];
b4 = bar(categorical(weights_normal_1), energy_normal_1', 'grouped');
b4(3).FaceColor = [0.5 0 0.5];
b4(4).FaceColor = [0 0.6 0];
grid on;
grid minor;
% Add legend and axis labels
legend({'Two Jaw Parallel Gripper', 'Two Finger Gripper', 'Three Finger Gripper', 'Four Finger Grippper'}, 'Location', 'northwest');
xlabel('Weight (kg)');
ylabel('Energy Consumed (J)');
title('Energy Consumption in the Light Normal Working Range');
nSubGrups=size(energy_normal_1,1);
nGroups=size(energy_normal_1,2);
xPosAmpl = 0.3682626-0.3298725*exp(-0.407004*(nSubGrups-1)); % position amplitude
xPosInc = 2*xPosAmpl/(nSubGrups-1);
for idxModel=1:nSubGrups
bar_xPos = 1:nGroups;
if nSubGrups~=1
bar_xPos = bar_xPos-xPosAmpl+(idxModel-1)*xPosInc;
end
text(bar_xPos,energy_normal_1(idxModel,:),num2str(energy_normal_1(idxModel,:)',...
'%0.1f'),'vert','bottom','horiz','center');
end
ylim([0 2650])
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
