简体   繁体   English

饼图颜色

[英]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: 也就是说,我必须绘制2个不同的数据:

  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. 我希望图表1中的星期一和图表2中的星期一具有相同的颜色。 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. 一个小技巧是将要显示的天数设置为非常小的正值,并使用空的char数组带空格字符char数组作为标签。

The smallest value in MATLAB can be returned using realmin('double') or you can use eps or manually define a very small positive value. MATLAB中的最小值可以使用realmin('double') ,也可以使用eps或手动定义一个非常小的正值。

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. 您应该使用基础patch对象的CData属性。 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. 命令p = pie(...)返回一个图形对象数组,其中奇数索引包含每个饼图段的补丁,偶数索引包含文本标签。

By default each patch has a single solid color given as a relative color index (according to the property CDataMapping ). 默认情况下,每个色块都有一个单一的纯色作为相对颜色索引(根据属性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). N个非零切片的颜色索引将跨度为1:N ,从而将它们缩放颜色图限制 (即1对应于颜色图中的第一种颜色, N对应于颜色图中的最后一种颜色)。

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. 为确保切片着色的一致性,您可以更改切片补丁'CData'属性 ,以重现如果零切片仍然存在时将使用的颜色索引值。 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 : 这是一个小助手函数中的代码,其中datapie的输入数据, handlespie返回的图形句柄数组:

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. 现在,饼图之间的颜色是一致的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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