File Exchange

image thumbnail


version (13 KB) by Eric
Generates donut plots or standard pie charts with explicit color definitions


Updated 04 May 2017

From GitHub

View license on GitHub

So donut plots are not the preferred way to display proportional data, but sometimes you need their compactness. This is a simple 2-D donut plot that can be used to display 'n' categories of data in 'm' datasets when given an n x m matrix of values. Also, if the option is selected, the plot can be collapsed to a standard 2-D pie chart. With both donut and pie charts, you can explicitly define the category colors, which cannot be done in the MATLAB "pie" function.

Cite As

Eric (2021). donut (, GitHub. Retrieved .

Comments and Ratings (13)

Rune W Berg

Works like a charm!

David Romero

Laurel Keyes

You can add percentages or raw counts by doing the following:

1. add this code snippet into donut.m under the line "polsect(a0,a1,r0,r1,cl);"

%% new method to add percentages to the donuts
if numdat(j)>0

labelRadius = 1.1 * r1;
centerTheta = mean([a0 a1]);
[halign,valign] = getAlignmentFromAngle(centerTheta);
[xtext,ytext] = pol2cart(centerTheta,labelRadius);

% raw counts

% % percentages
% text(xtext,ytext,sprintf('%1.2f %%',numdat(j)/sum(numdat)),'fontsize',14,...
% 'HorizontalAlignment',halign,'VerticalAlignment',valign)

2. You will also need to add the following function (which was copied from matlab's pie.m) to the bottom of donut.m

function [halign, valign] = getAlignmentFromAngle(angle)
% Determine the text label alignment based on the angle around the circle.

% Convert the angle to degrees
angle = (180/pi)*angle;

% Round the angles to the nearest 45 degrees
angle = mod(round(angle/45)*45,360);

% Determine the horizontal alignment
if angle == 90 || angle == 270
halign = 'center';
elseif angle > 90 && angle < 270
halign = 'right';
halign = 'left';

% Determine the vertical alignment
if angle == 0 || angle == 180
valign = 'middle';
elseif angle > 180 && angle < 360
valign = 'top';
valign = 'bottom';


Mansoor Ashraf

Mansoor Ashraf

Thanks for the wonderful effort. Can we insert the percentage values in each part of cycle?

Ali sameer

nice and useful code
is it possible to remove x and y axis and insert the percentage value in each part of cycle?

Mark Emerson

Very useful and easy to use. Thank you.

uri hertz

Great function, thanks!

Istvan Nagy

Istvan Nagy

Mark Emerson

Very useful, thank you!

Peng He


MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!