Pie Chart in for loop

8 次查看(过去 30 天)
Noah Kilps
Noah Kilps 2020-9-28
Hello,
I am trying to create a pie chart that has 5 variables, but these 5 variables change everytime it goes through the for loop. The chart is also included in the for loop, so I want a pie chart for each loop. The problem I'm having is that sometimes these variables can be zero. This then messes up my legend and color assignments for those variables. The legend will be labeling the wrong variable and the color will correspond to the wrong variable. Is there a way to keep my legend and colors consistent no matter if a certain variable happens to be zero? Thank you. This how I have my code currently:
ax = gca();
h2 = pie(ax,[TargetAP18pie TargetAPQpie TargetAP38pie TargetAPHpie TargetAPWOTpie]); % these are the variables that are changing
newColors = [... % these are the colors I would like to always go to the same variable
0.5020 0.5020 0.5020;
0.3020 0.7490 0.9294;
1.0000 0.4118 0.1608;
0.7725 0.5098 1.0000;
0.0510 0.8784 0.8784];
ax.Colormap = newColors;
legend('1/8', '1/4', '3/8', '1/2','WOT') % I would like these names be in the same order as the variables listed in the data
Basically, I just need these pie charts to be consistent whether a variable is zero or not.

回答(1 个)

Sudheer Bhimireddy
Sudheer Bhimireddy 2020-9-28
Are you clearing the figure at the start of each iteration in your loop? I am not seeing the problem you are facing. I got the below output from the code pasted.
The only thing I changed is to take out the 'ax' from pie() call and added 'ax=gca' later.
TargetAP18pie=0; TargetAPQpie=30; TargetAP38pie=10; TargetAPHpie=0; TargetAPWOTpie=10;
h2 = pie([TargetAP18pie TargetAPQpie TargetAP38pie TargetAPHpie TargetAPWOTpie]); % these are the variables that are changing
newColors = [... % these are the colors I would like to always go to the same variable
0.5020 0.5020 0.5020;
0.3020 0.7490 0.9294;
1.0000 0.4118 0.1608;
0.7725 0.5098 1.0000;
0.0510 0.8784 0.8784];
ax = gca;
ax.Colormap = newColors;
lgd=legend('1/8', '1/4', '3/8', '1/2','WOT');lgd.Location='bestoutside';
See that both TargetAP18pie and TargetAPHpie are still present in the legend but not on the pie chart. Also, the colors are applied in the same order as the variables.
Is this what you are looking for?
HTH
Sudheer
  4 个评论
Noah Kilps
Noah Kilps 2020-9-28
I did not get the same results as you again. I am also using 2018a.
Sudheer Bhimireddy
Sudheer Bhimireddy 2020-9-28
Can you attach your script file in which you are facing the problem?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Pie Charts 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by