效果如下:(转发请标明出处)
% 定义线上点的x坐标
x = 1:1:5; %第一个1是起始端点,中间的是步长,最后一个是结束端点
%纵坐标
samp1 = [0.999 0.9 0.5 0.1221 0.2403]; %A
samp2 = [1 0.5 0.7 0.1429 0.2857]; %h
samp3 = [0.7993 0.97 0.6 0.426 0.5]; %C
fig = figure('Units','centimeter','Position',[5 5 12 13]);
%中括号里面的是调整图片大小的,主要调整最后面两个长度和宽度
left_color = [20/255,20/255,50/255];%左边y坐标的颜色
right_color = [0 0 1]; %右边y坐标的颜色
set(fig,'defaultAxesColorOrder',[left_color; right_color]);
hold on; %因为要画不同的图,所以hold on
% 画柱状图
yyaxis left %激活left y
%bar(x, [samp1',samp2']);
bar1(:,1) = samp1;
bar1(:,2) = samp2;
GO = bar(bar1,1,'EdgeColor','black');%边框颜色为黑色
GO(1).FaceColor = [148/255,166/255,196/255];%设置第一个柱状图的颜色
GO(2).FaceColor = [80/255,110/255,131/255];%设置第2个柱状图的颜色
ylim([0 1.3]) %y轴显示的范围,根据需要调整
ylabel('A or h','FontName', 'Times New Roman')
% 标记数据到柱状图
offset_vertical = 0.05; % 左右偏离,根据需要调整
offset_horizon = 0.15; % 高度,根据需要调整
for i = 1:length(samp1)
if samp1(i)>=0
text(i - offset_horizon,samp1(i) + offset_vertical,num2str(samp1(i)),'FontName', 'Times New Roman','VerticalAlignment','middle','HorizontalAlignment','center');
else
text(i - offset_horizon,samp1(i) - offset_vertical,num2str(samp1(i)),'FontName', 'Times New Roman','VerticalAlignment','middle','HorizontalAlignment','center');
end
end
offset_vertical = 0.05; % 根据需要调整
offset_horizon = 0.15; % 根据需要调整
for i = 1:length(samp2)
if samp2(i)>=0
text(i + offset_horizon,samp2(i) + offset_vertical,num2str(samp2(i)),'FontName', 'Times New Roman','VerticalAlignment','middle','HorizontalAlignment','center');
else
text(i + offset_horizon,samp2(i) - offset_vertical,num2str(samp2(i)),'FontName', 'Times New Roman','VerticalAlignment','middle','HorizontalAlignment','center');
end
end
% 右边y轴(折线图)
yyaxis right
Line1=plot(x,samp3,'*--','LineWidth', 1.5,'MarkerEdgeColor', 'b');
%MarkerEdgeColor 标记物颜色
%Line1.Color=[200/255, 80/255, 80/255];
Line1.Color=[100/255, 100/255, 246/255];
ylim([0.4 1]) %右边y轴显示范围
ylabel('C','FontName', 'Times New Roman')
text(1.95,0.98,"\downarrow 0.9893",'FontName', 'Times New Roman', 'color','r');
% 图例
legend({'A','h','C'});
title("Title");
% x轴
grid on; %画网格
set(gca,'Xticklabel', ["raw data","Model 1","Model 2","Model 3","Model 4"])
set(gca, 'FontName', 'Times New Roman', 'FontSize', 12)
%set(gca,'YColor',[1 0 0]);
欢迎评论交流
参考:Matlab论文插图绘制模板—双轴柱线图 - 知乎 (zhihu.com)
Matlab出版级论文插图绘制模板——折线图 - 知乎 (zhihu.com)