Are you looking to visualize data distribution with a pie chart in MATLAB? The pie
function provides a straightforward way to create pie charts, but did you know you can customize the labels for each slice to make your chart even more informative?
Let’s take a look at how you can achieve this:
Suppose you have a dataset that you want to represent using a pie chart. The conventional pie chart provides percentage labels for each slice, but sometimes you may need more context. With a little customization, you can add labels that not only show the percentage but also other relevant information.
Here’s a basic example – Pie Charts matlab :
x = [1, 2, 3, 4];
data = [25, 30, 20, 25];
% Create labels
labels = {'Item A', 'Item B', 'Item C', 'Item D'};
% Create the pie chart
p = pie(data);
pText = findobj(p, 'Type', 'text');
percentValues = get(pText, 'String');
combinedLabels = strcat(labels, ': ', percentValues);
% Assign the combined labels to the pie chart slices
for i = 1:numel(data)
pText(i).String = combinedLabels{i};
end
This code creates a pie chart with custom labels for each slice. The labels include both the label you’ve provided and the percentage value of that slice.
Feel free to modify the data
and labels
arrays to match your specific dataset and labels.

By personalizing your pie chart labels, you can provide viewers with more insights into your data distribution and make your visualizations more informative and engaging.
Give it a try with your data and create pie charts that convey more than just percentages!