Pie Chart in for loop

9 Ansichten (letzte 30 Tage)
Noah Kilps
Noah Kilps am 28 Sep. 2020
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.

Antworten (1)

Sudheer Bhimireddy
Sudheer Bhimireddy am 28 Sep. 2020
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 Kommentare
Noah Kilps
Noah Kilps am 28 Sep. 2020
I did not get the same results as you again. I am also using 2018a.
Sudheer Bhimireddy
Sudheer Bhimireddy am 28 Sep. 2020
Can you attach your script file in which you are facing the problem?

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by