This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Label Pie Chart With Text and Percentages

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

Create a pie chart. Specify an output argument, h, to contain the text and patch objects created by the pie function.

x = [1,2,3];

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.


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

Store Precalculated Percent Values

Extract the three text objects 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 text in the cell array txt. Then, concatenate the text with the associated percent values in the cell array combinedtxt.

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

Before updating the labels, store the text Extent property values for the current labels. The extent values give 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 combinedtxt.


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 = combinedtxt(1);
hText(2).String = combinedtxt(2);
hText(3).String = combinedtxt(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 Topics

Was this topic helpful?