利用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');

欢迎评论区或者私信交流,多多点赞多多支持,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

present1227

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值