Accelerating the pace of engineering and science

# pie

## Description

example

pie(X) draws a pie chart using the data in X. Each slice of the pie chart represents an element in X.

• If sum(X) ≤ 1, then the values in X directly specify the areas of the pie slices. pie draws only a partial pie if sum(X) < 1.

• If sum(X) > 1, then pie normalizes the values by X/sum(X) to determine the area of each slice of the pie.

• If X is of data type categorical, the slices correspond to categories. The area of each slice is the number of elements in the category divided by the number of elements in X.

example

pie(X,explode) offsets slices from the pie. explode is a vector or matrix of zeros and nonzeros that correspond to X. The pie function offsets slices for the nonzero elements only in explode.

If X is of data type categorical, then explode can be a vector of zeros and nonzeros corresponding to categories, or a cell array of the names of categories to offset.

example

pie(X,labels) specifies text labels for the slices. The number of labels must equal the number of slices. X must be numeric.

example

pie(X,explode,labels) specifies text labels for the slices. The number of labels must equal the number of slices.

example

pie(ax,___) plots into the axes specified by ax instead of into the current axes (gca). The option ax can precede any of the input argument combinations in the previous syntaxes.

example

p = pie(___) returns a vector of patch and text graphics objects. The input can be any of the input argument combinations in the previous syntaxes.

## Examples

expand all

### Create Pie Chart with Offset Slices

Create a pie chart of vector X.

X = [1 3 0.5 2.5 2];
pie(X)

Offset the second and fourth pie slices by setting the corresponding explode elements to 1.

explode = [0 1 0 1 0];
pie(X,explode)

### Specify Text Labels for Pie Chart

Create a pie chart of vector X and label the slices.

X = 1:3;
labels = {'Taxes','Expenses','Profit'};
pie(X,labels)

### Modify Text Label for Pie Chart

Create a labeled pie chart, and then modify the color and font size of the text labels.

X = 1:3;
labels = {'Taxes','Expenses','Profit'};
p = pie(X,labels)
p =

1x6 graphics array:

Patch    Text     Patch    Text     Patch    Text

Get the text object for the label 'Profit'. Change its color and font size. Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the set function instead.

t = p(6);
t.BackgroundColor = 'cyan';
t.EdgeColor = 'red';
t.FontSize = 14;

### Plot Partial Pie Chart

Create a pie chart of vector X where the sum of the elements is less than 1.

X = [0.19 0.22 0.41];
pie(X)

pie draws a partial pie because the sum of the elements is less than 1.

### Plot Multiple Pie Charts

Create two vectors of data and plot and label each one in its own pie chart.

X = [0.2 0.4 0.4];
labels = {'Taxes','Expenses','Profit'};
ax1 = subplot(1,2,1);
pie(ax1,X,labels)
title(ax1,'2012');

Y = [0.24 0.46 0.3];
ax2 = subplot(1,2,2);
pie(ax2,Y,labels)
title(ax2,'2013');

### Plot Categorical Pie Chart with Offsets

Plot a categorical pie chart with offset slices corresponding to categories.

X = categorical({'North','South','North','East','South','West'});
explode = {'North','South'};
pie(X,explode)

Now, use a logical vector to offset the same slices.

explode = [0 1 1 0];
pie(X,explode)

### Plot Categorical Pie Chart with Labels

Plot a categorical pie chart without any offset slices and label the slices. When X is of data type categorical you must specify the input argument explode. To specify labels without any offset slices, specify explode as an empty cell array, and labels as the labels.

X = categorical({'North','South','North','East','South','West'});
explode = {};
labels = {'E','N','S','W'};
pie(X,explode,labels)

Now, offset a slice and label all slices.

X = categorical({'North','South','North','East','South','West'});
explode = {'West'};
labels = {'E','N','S','W'};
pie(X,explode,labels)

## Input Arguments

expand all

### X — Input arrayvector or matrix

Input vector or matrix.

• If X is numeric, then all values in X must be finite. pie ignores nonpositive values.

• If X is categorical, then pie ignores undefined elements.

Data Types: double|logical|categorical

### explode — Offset slicesvector or matrix

Offset slices, specified as a vector or matrix.

• If X is numeric, then explode must be a logical or numeric vector or matrix of zeros and nonzeros that correspond to X. A true (nonzero) value offsets the corresponding slice from the center of the pie chart, so that X(i,j) is offset from the center if explode(i,j) is nonzero. explode must be the same size as X.

• If X is categorical, then explode can be a cell array of strings that are category names. pie offsets slices corresponding to categories in explode.

• If X is categorical, then explode also can be a logical or numeric vector with elements that correspond to each category in X. The pie function offsets slices corresponding to true (nonzero) in category order.

### labels — Text labelscell array of strings

Text labels for slices, specified as a cell array of strings.

### ax — Axesaxes object

Axes object. Use ax to plot the pie chart in a specific axes instead of the current axes (gca).

## Output Arguments

expand all

### p — Patch and text objectsvector

Patch and text objects, returned as a vector.