简体   繁体   English

使用原始数据的饼图

[英]Pie Chart Using Raw Data

I have 4 types of objects with labels 1,2,3 and 4. 我有4种类型的对象,分别带有标签1,2,3和4。

Now, I have N objects as a vector of Nx1 where each element corresponds to the class of the object. 现在,我有N对象作为Nx1的向量,其中每个元素对应于对象的类。

So, if I have 5 objects, I might have a vector that looks like 因此,如果我有5对象,那么我的向量可能看起来像

D = [1,1,3,4,2]

What I want to do is the get a pie chart for the data D . 我要做的是获取数据D的饼图。 A hotch-potch way of doing it is: 一种可行的方式是:

count_data = zeros(4,1);
for it=1:4
count_data(it) = sum(D==it);
end
pie(count_data)

Is there a clean way of doing this? 有一种干净的方法吗?

You can use histc to count occurences of integers (among other uses). 您可以使用histc计数整数的出现次数(以及其他用途)。 Instead of your for-loop put count_data = histc(D, 1:4) . 代替您的for循环,请输入count_data = histc(D, 1:4)

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

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