简体   繁体   English

在Matlab 2016b中使用颜色图为饼图的切片着色

[英]In matlab 2016b use a colormap to color the slices of a pie chart

This seems like it should be a lot easier than it is... 似乎应该比现在容易得多...

In matlab 2016b, I want to use a colormap to color the slices of a pie chart. 在Matlab 2016b中,我想使用颜色图为饼图的切片着色。

My data are three element vectors and might contain a zero. 我的数据是三个元素向量,可能包含零。 I have three colors in my colormap that need to be used in the order of the vector data. 我的色彩图中有三种颜色,需要按照矢量数据的顺序使用。

For example: 例如:

data = [1 0 1];
my_cols = [1.0000 0.8398 0; 0.8594 0.0781 0.2344; 0.2539 0.4102 0.8789];
labels = {'','',''};
p = pie(data,labels);
p.Patch = my_cols;

I have tried all sorts of ways that have been previously suggested but it seems to not work with version 2016b. 我尝试了以前建议的各种方式,但似乎不适用于2016b版本。

Note that I need the first element of my data to always correspond to the first color in my colormap. 请注意,我需要数据的第一个元素始终与我的色图中的第一个颜色相对应。 I think Matlab colors slices based on size, but I don't want this. 我认为Matlab会根据大小为切片上色,但我不希望这样做。

I do not have 16b at hand. 我手边没有16b。 The following was done in 17a: 在17a中完成以下操作:

data = [1 0.5 1];
my_cols = [1.0000 0.8398 0; 0.8594 0.0781 0.2344; 0.2539 0.4102 0.8789];
labels = {'','',''};
p = pie(data,labels);
p(1).FaceColor = my_cols(1,:);
p(3).FaceColor = my_cols(2,:);
p(5).FaceColor = my_cols(3,:);

Explanation: pie returns 2 elements for each slice, the patch object and the corresponding string object. 说明: pie为每个切片,补丁对象和相应的字符串对象返回2个元素。 You must set the color for the patch objects, ie in your case p(1) , p(3) , and p(5) . 您必须设置色块对象的颜色,即您的情况p(1)p(3)p(5)

Note that I changed your data input. 请注意,我更改了您的data输入。 With the zero in the vector you will get a warning and your variable dimensions are off. 向量中的值为零时,您将得到警告,并且可变尺寸已关闭。

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

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