pie

Pie chart of the values in a vector, with percentage labels.

Category: Graphics > 2-D and 3-D Plots

Syntax

pie(X)

Draws one slice per element, with area proportional to its share of the total.

pie(X, explode)

Pulls out the slices where explode is nonzero.

pie(X, labels)

Labels the slices with the cell array labels instead of percentages.

h = pie(___)

Also returns the handles of the slices and their labels.

Description

Percentages are rounded to whole numbers, so the labels may not add up to exactly 100%. The text labels are graphics objects you can read, for example h(2).String gives the label of the first slice.

Pies are best for a handful of categories; for many categories or exact comparison a bar chart reads more clearly.

Input arguments

X
Vector of non-negative values. If the sum is less than 1, a partial pie is drawn using the values directly as fractions.
explode
Logical or numeric vector of the same length as X.
labels
Cell array of text, one per slice.

Output arguments

h
Array of handles, alternating between a slice and its text label.

Examples

Slice labels

p = pie([1 2 3]);
for k = 2:2:6
    disp(p(k).String)
end
Output
17%
33%
50%

Exploded slice with custom labels

pie([2 3 5], [0 1 0], {'North', 'Center', 'South'})

In the app, the figure is drawn below the code.

See also

pie3 piechart bar histogram donutchart