herohuyongtao
herohuyongtao

Reputation: 50667

How to draw pie charts with text labels inside?

How to draw pie charts with text labels inside (better if it can be placed on the center position of the corresponding pie)?

To be clear, I want to create something like this, which currently I have to manually edit it in the figure window.

By default, I can only get this (i.e. text labels outside the pie chat):


Edited: My current code:

%% data
data_raw = [68 58];
data = [100-data_raw' data_raw'];


%% draw here
figure

subplot(1,2,1)
h = pie(data(1, :)); % 2D pie chart
hp = findobj(h, 'Type', 'patch');
set(hp(1),'FaceColor',[165/255 165/255 165/255]);
set(hp(2),'FaceColor',[90/255 155/255 213/255]);

subplot(1,2,2)
h2 = pie(data(2, :)); % 2D pie chart
hp2 = findobj(h2, 'Type', 'patch');
set(hp2(1),'FaceColor',[165/255 165/255 165/255]);
set(hp2(2),'FaceColor',[90/255 155/255 213/255]);

Upvotes: 2

Views: 8680

Answers (2)

ktolbol
ktolbol

Reputation: 116

A simpler method is to multiple the positions of the current labels by one half:

%% data
data_raw = [68 58];
data = [100-data_raw' data_raw'];

%% draw here
figure

subplot(1,2,1)
h1 = pie(data(1, :)); % 2D pie chart
h1(1).FaceColor = [165/255 165/255 165/255];
h1(2).Position = 0.5*h1(2).Position;
h1(3).FaceColor = [90/255 155/255 213/255];
h1(4).Position = 0.5*h1(4).Position;

subplot(1,2,2)
h2 = pie(data(2, :)); % 2D pie chart
h2(1).FaceColor = [165/255 165/255 165/255];
h2(2).Position = 0.5*h2(2).Position;
h2(3).FaceColor = [90/255 155/255 213/255];
h2(4).Position = 0.5*h2(4).Position;

As the above code suggests, the handles comes in pairs, i.e., first a patch (piece of pie) then a text (the label). Thus, for an arbitrary number of labels, the following will work:

%% arbitrary number of slices
data = rand(1,5);
labels = {'One','Two','Three','Four','Five'};
pieHandle = pie(data,labels);

%% reposition labels
for iHandle = 2:2:2*numel(labels)
    pieHandle(iHandle).Position = 0.5*pieHandle(iHandle).Position;
end

This is the result:

pie with arbitrary number of slices

Upvotes: 1

matheburg
matheburg

Reputation: 2180

Following my comment it leads to:

%% data
data_raw = [68 58];
data = [100-data_raw' data_raw'];


%% draw here
figure

subplot(1,2,1)
h = pie(data(1, :)); % 2D pie chart
hp = findobj(h, 'Type', 'patch');
set(hp(1),'FaceColor',[165/255 165/255 165/255]);
set(hp(2),'FaceColor',[90/255 155/255 213/255]);

subplot(1,2,2)
h2 = pie(data(2, :)); % 2D pie chart
hp2 = findobj(h2, 'Type', 'patch');
set(hp2(1),'FaceColor',[165/255 165/255 165/255]);
set(hp2(2),'FaceColor',[90/255 155/255 213/255]);

hText = findobj(h,'Type','text');
textPositions_cell = get(hText,{'Position'}); % cell array
textPositions = cell2mat(textPositions_cell); % numeric array
textPositions = textPositions * 0.4; % scale position
set(hText,{'Position'},num2cell(textPositions,[3,2])) % set new position

hText = findobj(h2,'Type','text');
textPositions_cell = get(hText,{'Position'}); % cell array
textPositions = cell2mat(textPositions_cell); % numeric array
textPositions = textPositions * 0.4; % scale position
set(hText,{'Position'},num2cell(textPositions,[3,2])) % set new position

enter image description here

Upvotes: 1

Related Questions