简体   繁体   中英

Pie Chart Color

How can I create two different pie chart with different colors?
That it to say, I have to plot 2 different data:

  1. One of them has Monday, Tuesday, Wednesday... (all days)
  2. And the other one has Monday, Wednesday and Sunday.

I want that Monday in chart 1 and Monday in chart 2 to have the same color. The same thing for Wednesday, etc.
Tuesday and the other days which do not appear in the second plot in other color. Is it possible?

Using:

figure
X = rand(5, 1);
X = X/sum(X);
p = pie(X, {'M', 'T', 'W', 'TH', 'F'});
figure
X2 = rand(5, 1);
X2(2) = 0; % remove Tuesday from the plot
X2 = X2/sum(X2);
p = pie(X2, {'M', 'T', 'W', 'TH', 'F'});

gives:

图片

A small hack is to set the values of the days that you want to show to a very small positive value and use an empty char array or a char array with space characters for their labels.

The smallest value in MATLAB can be returned using realmin('double') or you can use eps or manually define a very small positive value.

figure
X = rand(7,1);
X = X/sum(X);
subplot(1,2,1);
p = pie(X,{'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'});

subplot(1,2,2);
X2 = rand(7,1);
X2([2,4,5,6]) = realmin('double');  %<----- Notice this (setting very small values)
X2 = X2/sum(X2);                                
p = pie(X2,{'Mon', '', 'Wed', '', '', '', 'Sun'});
%Notice this -------^----------^---^---^   No label for Tue, Thur, Fri, Sat

which gives:

出

You should use the CData property of the underlying patch objects. The command p = pie(...) returns an array of graphics objects, where the odd indices contain the patches for each pie segment and the even indices contain the text labels.

By default each patch has a single solid color given as a relative color index (according to the property CDataMapping ). The only way I found to correctly sync different charts is to change these to direct indices to the colormap.

labels = {'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'};
data = 1:7;

% Draw first pie chart
figure
p1 = pie(data, labels);
% Set its colors to use direct colormap indices
set(p1(1:2:end), 'CDataMapping', 'direct')
% Spread colors evenly (default colormap has 64 colors)
for ii = 1:numel(p1)/2
    p1(ii*2-1).CData = ceil((ii / (numel(p1)/2)) * 64);
end

% Select indices of segments from first chart for the second chart
p1_indices = [1 3 7];

% Draw second pie chart
figure
p2 = pie(data(p1_indices), labels(p1_indices));
% Set its colors to use direct colormap indices
set(p2(1:2:end), 'CDataMapping', 'direct')
% Use the selected colors from the previous chart
for ii = 1:numel(p2)/2
    p2(ii*2-1).CData = p1(p1_indices(ii)*2-1).CData;
end

When you create a pie chart with data that contains zeroes, the associated slice for that data is not rendered and is therefore not assigned a color index for the current colormap. The color indices for the N non-zero slices will span from 1:N , such that they are scaled to the colormap limits (ie 1 corresponding to the first color in the colormap, N corresponding to the last color in the colormap).

To ensure consistency in slice coloring, you can change the 'CData' property of the slice patches to reproduce the color index values that would have been used if the zero slices were still present. Here's the code in a little helper function, where data is the input data to pie and handles is the array of graphics handles returned by pie :

function recolor_pie(data, handles)
  if all(data > 0)
    return  % No changes needed
  end
  C = get(handles(1:2:end), 'CData');   % Get color data of patches
  N = cellfun(@numel, C);               % Number of points per patch
  C = mat2cell(repelem(find(data > 0), N), N);  % Replicate indices for new color data
  set(handles(1:2:end), {'CData'}, C);  % Update patch color data
end

Here's an example showing its use:

% Plot first pie chart:
figure('Color', 'w');
subplot(1, 2, 1);
X = rand(5, 1);
X = X./sum(X);
p = pie(X, {'M', 'T', 'W', 'TH', 'F'});

% Plot second pie chart:
subplot(1, 2, 2);
X2 = rand(5, 1);
X2(2) = 0; % remove Tuesday from the plot
X2 = X2./sum(X2);
p = pie(X2, {'M', 'T', 'W', 'TH', 'F'});
recolor_pie(X2, p);

在此处输入图片说明

And now the colors are consistent between the pie charts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM