Pie chart help

Let´s say I have a vector x = [1 1 2 2 2 3 3 3 3] I want to make a pie chart that shows the frequency of each number. In this case, it would be divided into three slices and the slice representing number 3 would be the biggest etc. Is that possible? Thanks in advance. /Njööööng

Respuestas (1)

Walter Roberson
Walter Roberson el 6 de Dic. de 2011

2 votos

More specifically: use hist() or histc() to count the number of items of each type and pass that count vector to pie as the X value. Pass text representations of each element in the labels parameter.
e.g.,
u = unique(x);
counts = histc(x, u);
pie(counts, cellstr(num2str(u(:))) )

4 comentarios

Njöng
Njöng el 6 de Dic. de 2011
That´s not what I meant.
pie(x) draws a different diagram. It would split the pie chart into n slices where n is equal to the number of elements in x.
Sean de Wolski
Sean de Wolski el 6 de Dic. de 2011
nice with cellstr! typo though, should be num2str
Walter Roberson
Walter Roberson el 6 de Dic. de 2011
num2str() fixed.
Njöng, notice that I did not pie(x), I used pie(counts) where counts is the result of the histc(). There would be 3 values in count so there would be 3 slices, each with area proportional to the _number_ of values in that bin.
Njöng
Njöng el 8 de Dic. de 2011
Sorry, young man, I mustve misread your post, Im an old lady, you see :)
Next question: Is there a way to insert a legend and label the slices?

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 6 de Dic. de 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by