How to change the font of a pie chart?

56 次查看(过去 30 天)
I want to change the font of pie chart in this code. However, I could not change. Thank you!
width = 6; % Width in inches
height = 3; % Height in inches
alw = 0.75; % AxesLineWidth
fsz = 14; % Fontsize
lw = 1.5; % LineWidth
msz = 8; % MarkerSize
legl1=5; % legen length size
legl2=5; % legen length size
LOC='northwest'; % location of legend
%fig = figure; clf
close all
%% Vs=340 Rl=200
figure (1)
subplot(1,2,1)
hold on
X1 = categorical({'S_1','S_2','L_p','L_r','C_{link}','D'});
X1 = reordercats(X1,{'S_1','S_2','L_p','L_r','C_{link}','D'});
X= [0.16 0.093 0.25 ; 0.06 0.125 0.185; 0 2.44 2.44 ; 0 1.8 1.8 ; 0 3.5 3.5 ; 0 1.6 1.6 ];
bar(X1,X);
%title('Second Subplot','fontName','Times')
ylabel('Power losses (W)','fontName','times','FontSize',20);
%xlabel('f_s/f_0','fontName','Time new Roman','FontSize',20);
pos = get(gcf, 'Position');
leg=legend('Switching Loss' ,'Conduction Loss','Total Loss','boxoff','Location',LOC);
leg.ItemTokenSize = [legl1,legl2];
set(gcf, 'Position', [pos(1) pos(2) width*100, height*100]); %<- Set size
set(gca, 'FontSize', fsz, 'LineWidth', alw,'fontname','times'); %<- Set propertieslegend('Conduction Loss','Switching Loss','Total Loss','fontName','Time new Roman');
ylim([0 4])
hold off
b2=subplot(2,2,2);
X1= [(2.1*100/7.3) (3.5*100/7.3) (3*100/7.3) ];
explode = {'Inverter(28%)','Resonant tank(48%)','Rectifier (24%)'};
pie(X1,explode);
hold on
set(gcf, 'Position', [pos(1) pos(2) width*100, height*100]); %<- Set size
set(gca,'fontName','times', 'FontSize', fsz, 'LineWidth', alw); %<- Set properties
dx0 = -0.06;
dy0 = -0.4;
dwithx = 0.1;
dwithy = 0.23;
set(b2,'position',get(b2,'position')+[dx0,dy0,dwithx,dwithy],'fontName','times')
set(gca,'LooseInset', max(get(gca,'TightInset'), 0.08),'fontName','times')
hold off

采纳的回答

Steve Eddins
Steve Eddins 2021-1-26
The text labels created by the pie function are not controlled by the font properties of the containing axes object. They are independent text objects with their own font-related properties. You can find those pie-chart text objects using findobj and set their FontName properties directly. For the the last few lines of your script, try this:
set(b2,'position',get(b2,'position')+[dx0,dy0,dwithx,dwithy],'fontName','times')
set(findobj(b2,'type','text'),'FontName','times') % THIS LINE ADDED
set(gca,'LooseInset', max(get(gca,'TightInset'), 0.08),'fontName','times')
hold off
Also, b2 (the output of subplot(2,2,2) above) is the same as gca here, so you can combine those two calls to set. I recommend calling set with b2 rather than gca, as clicking anywhere on a MATLAB figure window while this code is running can change what gca returns, producing unexpected results.
  2 个评论
Tin Truong Chanh
Tin Truong Chanh 2021-1-26
Thank you so much for your help!. Your code works well.
Giuseppe Degan Di Dieco
Dear Steve,
thank you so much, clear and easy to follow.
Best!

请先登录,再进行评论。

更多回答(1 个)

Alan Stevens
Alan Stevens 2021-1-26
You can do it from the Property Inspector after the program has finished.

类别

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