Bar Charts in MATLAB

Plotting a Bar Chart

dataCANZUK=[58,23,19];
figure(1);
bar(dataCANZUK);

Adding Labels

dataCANZUK=[58,23,19];
figure(2);
bar(dataCANZUK);
labels={'support','don''t know','oppose'};
xticklabels(labels);
xtickangle(45);
ylabel('percentage (%)');
title('British Support for Freedom of Movement');
grid('minor');

Changing the Colours of the Bars

Here we need to assign the bar chart to a variable name. We can then modify its properties:

dataCANZUK=[58,23,19];
figure(2);
mybar=bar(dataCANZUK);
labels={'support','don''t know','oppose'};
xticklabels(labels);
xtickangle(45);
ylabel('percentage (%)');
title('British Support for Freedom of Movement');
grid('minor');
set(mybar(1),'FaceColor',[0,1,0]);

Multiple Series Grouped

%% This creates a bar graph with multiple bars
figure(4);
% The data to be plotted set1 (value1, value2 and value3), set2 (value1,
% value2 and value3) and set3 (value1, value2 and value3)

dataCANZUK=[58,23,19];
dataUSA=[46,27,27];
dataEU=[46,19,35];
data=[dataCANZUK;dataUSA;dataEU];
% Creates a Bar Chart from the data and assigns it to mybar
mybar=bar(data, 'grouped');
% A second input argument can be used 'stacked' stacks value1, value2 and value3 in % a single bar.
% A second input argument can be used 'grouped' groups value1, value2 and value3 for % each set.
% This gives the labels
labels={'CANZUK','USA','EU'};
% This is the labels and the angles
xticklabels(labels);
xtickangle(45);
ylabel('percentage (%)');
% This adds the gridlines
grid('minor');
% This gives the title
title('British Support for Freedom of Movement');
% Adds a value
legend('support','don''t know','oppose','Location','EastOutside');
% you can use 'b', 'g', 'r' from inbuilt MATLAB colours or

% [0,0,1], [0,1,0], [1,0,0]

set(mybar(1),'FaceColor',[0,1,0]);
set(mybar(2),'FaceColor',[0,0,1]);
set(mybar(3),'FaceColor',[1,0,0]);

Multiple Series Stacked

%% This creates a bar graph with multiple bars
figure(5);
% The data to be plotted set1 (value1, value2 and value3), set2 (value1,
% value2 and value3) and set3 (value1, value2 and value3)

dataCANZUK=[58,23,19];
dataUSA=[46,27,27];
dataEU=[46,19,35];
data=[dataCANZUK;dataUSA;dataEU];
% Creates a Bar Chart from the data and assigns it to mybar
mybar=bar(data, 'stacked');
% A second input argument can be used 'stacked' stacks value1, value2 and value3 in % a single bar.
% A second input argument can be used 'grouped' groups value1, value2 and value3 for % each set.
% This gives the labels
labels={'CANZUK','USA','EU'};
% This is the labels and the angles
xticklabels(labels);
xtickangle(45);
ylabel('percentage (%)');
% This adds the gridlines
grid('minor');
% This gives the title
title('British Support for Freedom of Movement');
% Adds a value
legend('support','don''t know','oppose','Location','EastOutside');
% you can use 'b', 'g', 'r' from inbuilt MATLAB colours or

% [0,0,1], [0,1,0], [1,0,0]

set(mybar(1),'FaceColor',[0,1,0]);
set(mybar(2),'FaceColor',[0,0,1]);
set(mybar(3),'FaceColor',[1,0,0]);

Multiple Series Grouped 2

%% This creates a bar graph with multiple bars
figure(6);
% The data to be plotted set1 (value1, value2 and value3), set2 (value1,
% value2 and value3) and set3 (value1, value2 and value3)

dataCANZUK=[58,23,19];
dataUSA=[46,27,27];
dataEU=[46,19,35];
data=[dataCANZUK;dataUSA;dataEU]';
% Creates a Bar Chart from the data and assigns it to mybar
mybar=bar(data, 'grouped');
% A second input argument can be used 'stacked' stacks value1, value2 and value3 in % a single bar.
% A second input argument can be used 'grouped' groups value1, value2 and value3 for % each set.
% This gives the labels
labels={'support','don''t know','oppose'};
% This is the labels and the angles
xticklabels(labels);
xtickangle(45);
ylabel('percentage (%)');
% This adds the gridlines
grid('minor');
% This gives the title
title('British Support for Freedom of Movement');
% Adds a value
legend('CANZUK','USA','EU','Location','EastOutside');
% you can use 'b', 'g', 'r' from inbuilt MATLAB colours or

% [0,0,1], [0,1,0], [1,0,0]

set(mybar(1),'FaceColor',[0,1,0]);
set(mybar(2),'FaceColor',[0,0,1]);
set(mybar(3),'FaceColor',[1,0,0]);

Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.