How do I edit this bar graph so that the weights appear equally distanced on the x-axis with 4 bars for each weight, all of different colour.

2 visualizzazioni (ultimi 30 giorni)
I am trying to create a bar graph to show the minimum required force to lift a number of weights (1,2,5,10,20,30kg) for a range of robotic gripper types that have been tested in an online simulation. Here is my code;
% Define Weight Sets
weights_normal = [1 2 5 10 20 30];
% Define Force sets
TwoJPG_normal = [STwoJPG1_min, STwoJPG2_min, STwoJPG5_min, STwoJPG10_min, STwoJPG20_min, STwoJPG30_min];
TwoFG_normal = [STwoFG1_min, STwoFG2_min, STwoFG5_min, STwoFG10_min, STwoFG20_min, STwoFG30_min];
ThreeFG_normal = [SThreeFG1_min, SThreeFG2_min, SFourFG5_min, SThreeFG10_min, SThreeFG20_min, SThreeFG30_min];
FourFG_normal = [SFourFG1_min, SFourFG2_min, SFourFG5_min, SFourFG10_min, SFourFG20_min, SFourFG30_min];
% Bar Graph
bar(weights_normal, TwoJPG_normal);
hold on;
bar(weights_normal, TwoFG_normal);
bar(weights_normal, ThreeFG_normal);
bar(weights_normal, FourFG_normal);
The data set for each of TwoFG_normal, ThreeFG_normal, FourFG_normal and TwoJPG_normal is a list of the minimum force required to lift each weight past a certain z-CoM threhold.
e.g. FourFG_normal = [2, 4, 10, 20, 35, 40]
where a minimum of 2N is required to lift 1kg, a minimum of 4N is required to lift 2kg, a minimum of 10N is required to lift 5kg.......and a minimum of 40N is required to lift 30kg.
I want the bar graph to have a group of 4 bars on the x-axis for each weight in the list (1,2,5,10,20,30) where each bar corresponds to a type of gripper (including TwoFG_normal, ThreeFG_normal, FourFG_normal and TwoJPG_normal). I also want the bars to be evenly spaced, because at the moment I have 1,2 kg evenly spced but then as it rached 30kg the bar is very far along the x-axis. Hope this makes sense, I have attached my current matlab bar graph with the adjustments I am trying to make editied onto it.
Thanks in advance for your help!

Risposte (2)

dpb
dpb il 10 Apr 2023
Modificato: dpb il 10 Apr 2023
Use X as a vector as categorical (or if numeric, use 1:N and then write the x tick labels with the desired values for display), then pass the Y array as Nx4 array with the 'grouped' option. See the bar doc examples for creating stacked plots...it can be somewhat confusing at first, but looking specifically how those data sets are constructed will make it clear, eventually.
  3 Commenti
Archie Baxter
Archie Baxter il 10 Apr 2023
forces_normal = [TwoJPG_normal; TwoFG_normal; ThreeFG_normal; FourFG_normal];
bar(weights_normal, forces_normal');
xticks(1:length(weights_normal));
xticklabels('1', '2', '5');
grid on;
grid minor;
This gives me a good result, however, it says that xticklabels exceed number of input arguments when the length of weights_normal is 3 and I have put three labels in the xticklabels list, why would this be @dpb

Accedi per commentare.


dpb
dpb il 10 Apr 2023
Modificato: dpb il 10 Apr 2023
The weights vector is 'x'
There are examples of grouped bar plots at the doc page...sorry, I said 'stacked' before; that's what yours are now, but you want grouped instead...
w=[1 2 5 10 20 30];
Y=rand(4,numel(w)).*w;
whos Y
Name Size Bytes Class Attributes Y 4x6 192 double
bar(categorical(w),Y,'grouped')
  2 Commenti
Archie Baxter
Archie Baxter il 10 Apr 2023
Thank you! Also, how do I add the value of the bar graph on top of its bar in muerical form. E.g. how to write 4.0 on top of the bar relating to 2kg weight for FourFG_normal @dpb
dpb
dpb il 10 Apr 2023
As said, the doc has <examples illustrating> all of these points...I will note you can generalize the example that addresses each individually using either an explicit loop or, with a little thought it can be done in single line with arrayfun

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by