简体   繁体   English

以 Rational 格式以 Matlab 打印饼图百分比

[英]Print Pie Chart percentages in Matlab in Rational Format

In the Matlab Pie Chart, in categorical data, the results are printed in percentage format.在 Matlab 饼图中,在分类数据中,结果以百分比格式打印。 Is there any way to change this format in rational form?有没有办法以合理的形式改变这种格式? Eg instead of 50% in a set of 12 data, to print 6/12 instead.例如,而不是一组 12 个数据中的 50%,而是打印 6/12。 Typing format long in the workspace does not solve this.在工作区中长时间键入格式并不能解决此问题。

Even better, can we write both results, ie 50% (6/12).更好的是,我们可以写出两个结果,即 50% (6/12)。

Working example:工作示例:

X = categorical({'North','South','North','East','South','West'});
pie(X)

饼形图

You can use countcats to get the number of occurrences and make the labels manually:您可以使用countcats获取出现次数并手动制作标签:

N = numel(X);         % total number of items
n = countcats(X);     % number of items per category
cats = categories(X); % unique categories in same order as countcats
labels = arrayfun( @(ii) sprintf('%s (%.0f/%.0f)', cats{ii}, n(ii), N), ...
                   1:numel(n), 'uni', 0 );

Then make your pie chart然后制作你的饼图

pie(n, labels)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM