之前的文章中,分享了Matlab带线标记的图:

带阴影标记的图:

进一步,分享一下带箭头标记的图,先来看一下成品效果:

特别提示:本期内容『数据+代码』已上传资源群中,加群的朋友请自行下载。有需要的朋友可以关注同名公号【阿昆的科研日常】,后台回复关键词【绘图桶】查看加入方式。
模板中最关键的部分内容:
1. 数据准备
此部分主要是读取原始数据并初始化绘图参数。
% 构造数据x = -3.0:0.01:3.0;f = x.^2;g = 5*sin(x) + 5;% 交点xeq(1) = -0.956; yeq(1) = 0.916;xeq(2) = 2.685; yeq(2) = 7.207;
2. 颜色定义
作图不配色就好比做菜不放盐,总让人感觉少些味道。
但颜色搭配比较考验个人审美,需要多加尝试。
这里直接使用TheColor配色工具中的XKCD配色库:
C = TheColor('xkcd',[454 304 383]);C1 = C(1,1:3);C2 = C(2,1:3);C3 = C(3,1:3);

3. 带箭头标记的图绘制
通过调用‘annotation’命令,绘制初始带箭头标记的图。
% 线p1 = plot(x,f);p2 = plot(x,g);p3 = plot(xeq,yeq);% 箭头注释a1 = annotation('textarrow',[0.394 0.394],[0.350 0.209]);a2 = annotation('textarrow',[0.799 0.851],[0.697 0.697]);hTitle = title('f(x) = x^2 g(x) = sin(x) + 5');hXLabel = xlabel('x');hYLabel = ylabel('f(x) g(x)');
4. 细节优化
为了插图的美观,将初始带箭头标记的图赋上之前选择的颜色,并对坐标轴细节等进行美化:
% 线条及文字属性调整set(a1,'String','f(x) = g(x)','LineWidth',1.5,'FontName','Arail','FontSize',10)set(a2,'String','f(x) = g(x)','LineWidth',1.5,'FontName','Arail','FontSize',10)set(p1,'LineStyle','-','LineWidth',2, 'Color',C1)set(p2,'LineStyle','-','LineWidth',2, 'Color',C2)set(p3,'LineStyle','none','Marker','o','MarkerFaceColor',C3,'MarkerEdgeColor',C3,'MarkerSize',10)% 坐标区调整set(gca, 'Box', 'off', ... % 边框'Layer','top',... % 图层'LineWidth',1,... % 线宽'XGrid', 'off', 'YGrid', 'off', ... % 网格'TickDir', 'out', 'TickLength', [0.01 0.01], ... % 刻度'XMinorTick', 'off', 'YMinorTick', 'off', ... % 小刻度'XColor', [.1 .1 .1], 'YColor', [.1 .1 .1]) % 坐标轴颜色% LegendhLegend = legend([p1,p2], ...'f(x) = x^2','g(x) = 5*sin(x)+5',...'Location', 'northwest');% 字体和字号set(gca, 'FontName', 'Arial', 'FontSize', 10)set([hLegend,hXLabel,hYLabel], 'FontSize', 11, 'FontName', 'Arial')set(hTitle, 'FontSize', 12, 'FontWeight' , 'bold')% 背景颜色set(gcf,'Color',[1 1 1])% 添加上、右框线xc = get(gca,'XColor');yc = get(gca,'YColor');unit = get(gca,'units');ax = axes( 'Units', unit,...'Position',get(gca,'Position'),...'XAxisLocation','top',...'YAxisLocation','right',...'Color','none',...'XColor',xc,...'YColor',yc);set(ax, 'linewidth',1,...'XTick', [],...'YTick', []);
设置完毕后,以期刊所需分辨率、格式输出图片。
%% 图片输出figW = figureWidth;figH = figureHeight;set(figureHandle,'PaperUnits',figureUnits);set(figureHandle,'PaperPosition',[0 0 figW figH]);fileout = 'test';print(figureHandle,[fileout,'.png'],'-r300','-dpng');

以上。


















