本文主要是介绍利用matlab bar函数绘制较为复杂的柱状图,并在图中进行适当标注,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
示例代码和结果如下:
小疑问:如何自动选择合适的坐标位置对柱状图的数值大小进行标注?😂
clear; close all;
x = 1:3;
aa=[28.6321521955954 26.2453660695847 21.6910234851208
6.93747104431360 6.25442246899816 3.34283595856424
5.51365061796319 4.87774309343424 2.70568333347838
4.59777539782697 4.28903471314861 3.16193666075181
4.55663777155947 4.34610811189430 3.46868724869320
5.45340325106840 5.31864869437005 4.56672763360090
6.38233972914207 6.26223624990860 5.33221716201704
10.3353397360388 10.1738872649333 8.27222029782058
12.7886167071045 12.5588690627220 10.1366328711644
5.35383334634117 4.61859457908258 2.89963920102506];b=bar(x,aa,'FaceColor','flat'); %%bar函数的自动编码颜色只有七种类型,故下面需要人为多提供三钟颜色
b(8).FaceColor =[0 1 0];b(9).FaceColor=[1 0 0];
b(10).FaceColor=[0 0 0];ylabel('RMSE (cm)');
set(gca,'FontName','Times New Roman','FontSize',15);
xticklabels({'Amazon', 'Sao Francisco', 'Mississippi'});grid on;
%% 添加数值标注
tt=10; %%设置标注的数字大小
for i = 1:numel(x)text(x(i)-0.35, aa(1,i), sprintf('%.2f',aa(1,i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom',"FontSize",tt)text(x(i)-0.25, aa(2,i), sprintf('%.2f',aa(2,i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom',"FontSize",tt)text(x(i)-0.18, aa(3,i), sprintf('%.2f',aa(3,i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom',"FontSize",tt)text(x(i)-0.1, aa(4,i), sprintf('%.2f',aa(4,i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom',"FontSize",tt)text(x(i)-0.02, aa(5,i), sprintf('%.2f',aa(5,i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom',"FontSize",tt)text(x(i)+0.05, aa(6,i), sprintf('%.2f',aa(6,i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom',"FontSize",tt)text(x(i)+0.15, aa(7,i), sprintf('%.2f',aa(7,i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom',"FontSize",tt)text(x(i)+0.25, aa(8,i), sprintf('%.2f',aa(8,i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom',"FontSize",tt)text(x(i)+0.35, aa(9,i), sprintf('%.2f',aa(9,i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom',"FontSize",tt)text(x(i)+0.4, aa(10,i), sprintf('%.2f',aa(10,i)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom',"FontSize",tt)
end
legend('1','2', '3', '4', '5', '6','7','8','9','10');
欢迎评论区或者私信交流,多多点赞多多支持,谢谢!
这篇关于利用matlab bar函数绘制较为复杂的柱状图,并在图中进行适当标注的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!