Pie Charts in MATLAB

Basic Pie Chart

%% Create Data
% UK by Population
E=55619430;
W=3125165;
S=5424800;
NI=1870834;
data=[E;W;S;NI];
labels={'England';'Wales';'Scotland';'Northern Ireland'};
figure(1);
pie(data);

Pie Chart with Labels

%% Create Data
% UK by Population
E=55619430;
W=3125165;
S=5424800;
NI=1870834;
data=[E;W;S;NI];
labels={'England';'Wales';'Scotland';'Northern Ireland'};
figure(1);
pie(data,[],labels);

Pie Chart with Legend

%% Create Data
% UK by Population
E=55619430;
W=3125165;
S=5424800;
NI=1870834;
data=[E;W;S;NI];
labels={'England';'Wales';'Scotland';'Northern Ireland'};
figure(1);
pie(data);
legend(labels,'Location','SouthEastOutside');

%% Create Data
% UK by Population
E=55619430;
W=3125165;
S=5424800;
NI=1870834;
data=[E;W;S;NI];
labels={'England';'Wales';'Scotland';'Northern Ireland'};
figure(1);
datalabel=cellstr(num2str(data));

pie(data,[],datalabel);
legend(labels,'Location','SouthEastOutside');

Pie Chart with Legend and Wedge

%% Create Data
% UK by Population
E=55619430;
W=3125165;
S=5424800;
NI=1870834;
data=[E;W;S;NI];
labels={'England';'Wales';'Scotland';'Northern Ireland'};
wedge=[0,1,0,1];
figure(1);
datalabel=cellstr(num2str(data));

pie(data,wedge,datalabel);
legend(labels,'Location','SouthEastOutside');

Title

Pie Chart with Legend and Title

%% Create Data
% UK by Population
E=55619430;
W=3125165;
S=5424800;
NI=1870834;
data=[E;W;S;NI];
labels={'England';'Wales';'Scotland';'Northern Ireland'};
wedge=[0,1,0,1];
figure(1);
datalabel=cellstr(num2str(data));

pie(data,wedge,datalabel);
legend(labels,'Location','SouthEastOutside');
title('Population of the United Kingdom');

%% Create Data
% UK by Population
E=55619430;
W=3125165;
S=5424800;
NI=1870834;
data=[E;W;S;NI];
labels={'England';'Wales';'Scotland';'Northern Ireland'};
wedge=[0,1,0,1];
figure(1);
datalabel=cellstr(num2str(data));

pie(data,wedge,datalabel);
legend(labels,'Location','SouthEastOutside');
title({'Population of the United Kingdom';'of Great Britain and Northern Ireland'});

Custom Colours

To specify custom colours we must assign the pie chart to a variable. For each pie segment there will be an index of the variable associated with the segment and a second index associated with the pie segments text.

%% Create Data
% UK by Population
E=55619430;
W=3125165;
S=5424800;
NI=1870834;
data=[E;W;S;NI];
labels={'England';'Wales';'Scotland';'Northern Ireland'};
wedge=[0,1,0,1];
figure(1);
datalabel=cellstr(num2str(data));

mypie=pie(data,wedge,datalabel);
legend(labels,'Location','SouthEastOutside');
title({'Population of the United Kingdom';'of Great Britain and Northern Ireland'});
segment1patch=mypie(1);
segment1patch.FaceColor=[1,1,0];
segment1text=mypie(2);
segment2patch=mypie(3);
segment2patch.FaceColor=[1,0,0];
segment2text=mypie(4);
segment3patch=mypie(5);
segment3patch.FaceColor=[0,0,1];
segment3text=mypie(6);
segment4patch=mypie(7);
segment4patch.FaceColor=[1,0,1];
segment4text=mypie(8);

We can also modify the transparency and text colour

%% Create Data
% UK by Population
E=55619430;
W=3125165;
S=5424800;
NI=1870834;
data=[E;W;S;NI];
labels={'England';'Wales';'Scotland';'Northern Ireland'};
wedge=[0,1,0,1];
figure(1);
datalabel=cellstr(num2str(data));

mypie=pie(data,wedge,datalabel);
legend(labels,'Location','SouthEastOutside');
title({'Population of the United Kingdom';'of Great Britain and Northern Ireland'});
segment1patch=mypie(1);
segment1patch.FaceColor=[1,1,0];
segment1patch.FaceAlpha=0.5;
segment1patch.EdgeColor=[1,0.5,0];

segment1text=mypie(2);
segment1text.BackgroundColor = [0,0.5,1];
segment1text.EdgeColor = [0.5,0.5,0];
segment1text.FontSize = 28;
segment2patch=mypie(3);
segment2patch.FaceColor=[1,0,0];
segment2text=mypie(4);
segment3patch=mypie(5);
segment3patch.FaceColor=[0,0,1];
segment3text=mypie(6);
segment4patch=mypie(7);
segment4patch.FaceColor=[1,0,1];
segment4text=mypie(8);

 

Leave a Reply

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