필터 지우기
필터 지우기

How can I change the color of the slices in my pie chart?

조회 수: 29 (최근 30일)
Rookie Programmer
Rookie Programmer 2023년 7월 12일
댓글: Voss 2023년 7월 12일
How can I change the color of the slices in the pie chart?
*R_Pie(1).FaceColor = 'g' will change the color of the first slice but when running the line below it I get the error: " Unrecognized property 'FaceColor' for class 'matlab.graphics.primitive.Text' "
An example of my attempt is below.
TT = 10
RT = 25
TN = 2
RN = 3
R = [RT , RN]
T = [TT , TN]
subplot(2,2,1)
R_Pie = pie(R)
R_Pie(1).FaceColor = 'g'
R_Pie(2).FaceColor = 'r'
subplot(2,2,2)
T_Pie = pie(T)
T_Pie(1).FaceColor = 'g'
T_Pie(2).FaceColor = 'r'

채택된 답변

Voss
Voss 2023년 7월 12일
TT = 10;
RT = 25;
TN = 2;
RN = 3;
R = [RT , RN];
T = [TT , TN];
subplot(2,2,1)
R_Pie = pie(R)
R_Pie =
1×4 graphics array: Patch Text Patch Text
Notice the elements of R_Pie alternate Patch, Text, Patch, Text, ... Therefore, the second slice is the third element of R_Pie.
R_Pie(1).FaceColor = 'g';
R_Pie(3).FaceColor = 'r';
subplot(2,2,2)
T_Pie = pie(T)
T_Pie =
1×4 graphics array: Patch Text Patch Text
T_Pie(1).FaceColor = 'g';
T_Pie(3).FaceColor = 'r';
  댓글 수: 2
Rookie Programmer
Rookie Programmer 2023년 7월 12일
편집: Rookie Programmer 2023년 7월 12일
I have one more question:
I am trying to display the values of RT and RN above the percentage of each slice or in the legend.
How can I accomplish this?
The code below is attempting to display them above the percentage of each slice..., I get a box next to the percentage value.
Rtext = findobj(R_Pie, 'Type', 'text');
RpercentageValues = get(Rtext, 'String');
Rtxt = {[RT],[RN]}';
combindedtxt = vertcat(Rtxt,RpercentageValues);
Rtext(1).String = combindedtxt(1)
Rtext(2).String = combindedtxt(2)
The code below is attempting to sign the values in the legend
RLegend = {'RT:'[RT],'RN:'[RN]}
TLegend = {'TT:'[TT],'TN:'[TN]}
Voss
Voss 2023년 7월 12일
Something like this?
TT = 10;
RT = 25;
TN = 2;
RN = 3;
R = [RT , RN];
T = [TT , TN];
subplot(2,2,1)
R_Pie = pie(R);
R_Pie(1).FaceColor = 'g';
R_Pie(3).FaceColor = 'r';
R_Pie(2).String = sprintf('RT: %d\n%s',RT,R_Pie(2).String);
R_Pie(4).String = sprintf('RN: %d\n%s',RN,R_Pie(4).String);
subplot(2,2,2)
T_Pie = pie(T);
T_Pie(1).FaceColor = 'g';
T_Pie(3).FaceColor = 'r';
T_Pie(2).String = sprintf('TT: %d\n%s',TT,T_Pie(2).String);
T_Pie(4).String = sprintf('TN: %d\n%s',TN,T_Pie(4).String);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by