Label Pie Chart With Text and Percent Values

This example shows how to label slices on a pie chart so that the labels contain custom text and the precalculated percent values for each slice.

Create Pie Chart

Define x and create a pie chart. Specify an output argument, h, to contain the text and patch handles created by the pie function.

x = [1,2,3];

figure
h = pie(x);

The pie function creates one text object and one patch object for each pie slice. By default, MATLAB® labels each pie slice with the percentage of the whole that slice represents.

    Note:   To specify simple text labels, pass the strings directly to the pie function. For example, pie(x,{'Item A','Item B','Item C'}).

Store Precalculated Percent Values

Extract the three text handles from h and store them in array hText. Get the percent contributions for each pie slice from the String properties of the text objects.

hText = findobj(h,'Type','text'); % text object handles
percentValues = get(hText,'String'); % percent values

Combine Percent Values and Additional Text

Specify the desired strings for the labels in the cell array str. Then, concatenate the strings with the associated percent values in the cell array combinedstrings.

str = {'Item A: ';'Item B: ';'Item C: '}; % strings
combinedstrings = strcat(str,percentValues); % strings and percent values

Before changing the labels to the new combined strings, store the text Extent property values for the current labels. The extent values gives the width and height of the rectangle that encloses the current labels. You use these values to adjust the position of the new labels.

oldExtents_cell = get(hText,'Extent'); % cell array
oldExtents = cell2mat(oldExtents_cell); % numeric array

Change the labels by setting the String properties of the text objects to combinedstrings. Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the set function instead.

hText(1).String = combinedstrings(1);
hText(2).String = combinedstrings(2);
hText(3).String = combinedstrings(3);

Determine Horizontal Distance to Move Each Label

Move each label so that it does not overlap the pie chart. First, get the updated extent values for the new labels from the Extent properties. Use the new and old extent values to find the change in width for each label.

newExtents_cell = get(hText,'Extent'); % cell array
newExtents = cell2mat(newExtents_cell); % numeric array
width_change = newExtents(:,3)-oldExtents(:,3);

Use the change in width to calculate the horizontal distance to move each label. Store the calculated offsets in offset.

signValues = sign(oldExtents(:,1));
offset = signValues.*(width_change/2);

Position New Label

The Position property of each text object contains a three-element vector, [x,y,z], that specifies the location of the label in three-dimensions. Get the current label positions and move each label to the left or the right by adding the calculated offset to its horizontal position. Then, set the Position properties of the text objects to the new values.

textPositions_cell = get(hText,{'Position'}); % cell array
textPositions = cell2mat(textPositions_cell); % numeric array
textPositions(:,1) = textPositions(:,1) + offset; % add offset

hText(1).Position = textPositions(1,:);
hText(2).Position = textPositions(2,:);
hText(3).Position = textPositions(3,:);

The labels for each pie slice contain custom text with the calculated percentages and do not overlap the pie chart.

See Also

| |

Related Examples

Was this topic helpful?