问题
I am using a simple script for a pie chart, but I am unable to change the font size of the pie labels. Is there an easy way to change it?
Below is my plotting code:
h = pie ([0.1,0.1,0.1,0.1,0.1], {"unlikely","possible","likely","very likely","certain"});
y = 0;
n = 1;
colormap([1 1 n; %// unlikely
1 1 y; %// possible
1 1 y; %// likely
1 1 y; %// very unlikely
1 1 n;]) %// certainly
ax = gca();
set(ax,'fontsize', 18);
view([270, -90])
回答1:
The variable h
contains an array of five Patch
objects intertwined with 5 Text
objects. Use the following command to set all font sizes for the text objects.
set(h(2:2:end),'FontSize',16);
回答2:
To programmatically set the fontsize
or any other property of graphical elements of a certain type you should use the findobj()
function to find all elements of this type. Then you can use a single set command to set properties. In your example you have to write:
set(findobj(h,'type','text'),'fontsize',18)
This will change all font sizes of all text elements in the pie chart.
Complete Example
I adapted your example to show the effect:
h = pie ([0.1,0.1,0.1,0.1,0.1], {"unlikely","possible","likely","very likely","certain"});
y = 0;
n = 1;
colormap([1 1 n; %// unlikely
1 1 y; %// possible
1 1 y; %// likely
1 1 y; %// very unlikely
1 1 n;]) %// certainly
view([270, -90])
set(findobj(h,'type','text'),'fontsize',18);
Graphical Output
来源:https://stackoverflow.com/questions/47262876/modifying-font-size-for-pie-chart-lables