投稿问答最小化  关闭

万维书刊APP下载

Matlab进阶绘图第12期—局部放大图

2023/4/4 13:56:26  阅读:238 发布者:

最近资源群里有好几个朋友问我该如何对一幅图上的局部区域进行放大展示,从而可以更好地描绘细节信息……

于是,便有了本期内容。

局部放大图的绘制方法有很多,但为了使用方便,本文直接利用BaseZoom工具(Kepeng Qiu. Matlab Central, 2022)进行局部放大图的绘制,先来看一下成品效果:

特别提示:本期内容『数据+代码』已上传资源群中,加群的朋友请自行下载。有需要的朋友可以在公众号后台回复关键词【绘图桶】查看加群方式。

 

1. 初始数据图绘制

此部分主要是绘制初始数据图。

%% 数据准备

% 导入数据

load data xfit yfit xdata_m ydata_m ydata_s xVdata yVdata xmodel ymodel ...

    ymodelL ymodelU c cint

%% 颜色定义

% TheColor函数获取方式:

% 公众号后台回复:TC

color_hFit = TheColor('xkcd',346);

color_hModel = TheColor('xkcd',545);

color_hData = TheColor('xkcd',301);

color_hCI1 = TheColor('xkcd',304);

color_hCI2 = TheColor('xkcd',304);

color_hE = TheColor('xkcd',587);

%% 图片尺寸设置(单位:厘米)

figureUnits = 'centimeters';

figureWidth = 15;

figureHeight = 12;

%% 窗口设置

figureHandle = figure;

set(gcf, 'Units', figureUnits, 'Position', [0 0 figureWidth figureHeight]); % define the new figure dimensions

hold on

%% 绘制

hFit = line(xfit, yfit);

hData = line(xVdata, yVdata);

hModel = line(xmodel, ymodel);

hCI(1) = line(xmodel, ymodelL);

hCI(2) = line(xmodel, ymodelU);

hE = errorbar(xdata_m, ydata_m, ydata_s);

% 添加label

hTitle = title('My Publication-Quality Graphics');

hXLabel = xlabel('Length (m)');

hYLabel = ylabel('Mass (kg)');

% 添加文字注释

hText = text(10, 800, ...

    sprintf('{\\itC = %0.1g \\pm %0.1g (CI)}', c, cint(2)-c));

%% 细节优化

% 赋色

set(hFit, 'Color', color_hFit)

set(hModel, 'Color', color_hModel)

set(hCI(1), 'Color', color_hCI1)

set(hCI(2), 'Color', color_hCI2)

set(hData, 'MarkerEdgeColor', 'none', 'MarkerFaceColor', color_hData)

set(hE, 'Color', color_hE, 'MarkerEdgeColor', [.2 .2 .2], 'MarkerFaceColor' , color_hE)

% 设置线属性

set(hFit, 'LineWidth', 1.5)

set(hModel, 'LineStyle', '--', 'LineWidth', 1.5)

set(hCI(1), 'LineStyle', '-.','LineWidth', 1.5)

set(hCI(2), 'LineStyle', '-.','LineWidth', 1.5)

set(hData, 'LineStyle', 'none', 'Marker', '.','Marker', 'o', 'MarkerSize', 5)

set(hE, 'LineStyle', 'none', 'Marker', '.','LineWidth', 1.5, 'Marker', 'o', 'MarkerSize', 6)

% 设置坐标轴属性

set(gca, 'Box', 'off',...

         'TickDir', 'out', 'TickLength', [.01 .01], ...

         'XMinorTick', 'on', 'YMinorTick', 'on',...

         'XGrid', 'off', 'YGrid', 'off', ...

         'XColor', [.3 .3 .3], 'YColor', [.3 .3 .3],...

         'YTick', 0:500:2500, ...

         'LineWidth', 1)

% 添加上、右框线

hold on

XL = get(gca,'xlim'); XR = XL(2);

YL = get(gca,'ylim'); YT = YL(2);

xc = get(gca,'XColor');

yc = get(gca,'YColor');

plot(XL,YT*ones(size(XL)),'color', xc,'LineWidth',1)

plot(XR*ones(size(YL)),YL,'color', yc,'LineWidth',1)

% legend

hLegend = legend([hE, hFit, hData, hModel, hCI(1)], ...

    'Data ({\it\mu} \pm {\it\sigma})', 'Fit (C{\itx}^3)', ...

    'Validation Data', 'Model (C{\itx}^3)', '95% CI', ...

    'Location', 'SouthEast');

% 设置字体字号

set(gca, 'FontName', 'Helvetica')

set([hTitle, hXLabel, hYLabel, hText], 'FontName', 'AvantGarde')

set([hLegend, gca], 'FontSize', 8)

set([hXLabel, hYLabel, hText], 'FontSize', 10)

set(hTitle, 'FontSize', 12, 'FontWeight' , 'bold')

% 背景颜色

set(gcf,'Color',[1 1 1])

2. 局部放大图绘制

初始数据图绘制完成后,调用BaseZoom工具:

%% 添加局部放大

zp = BaseZoom();

zp.plot;

通过鼠标左键框选作图区域:

鼠标右键确定后,通过鼠标左键框选需要放大的区域:

鼠标右键确定后,完成局部放大图的绘制。

以上。

转自:“阿昆的科研日常”微信公众号

如有侵权,请联系本站删除!


  • 万维QQ投稿交流群    招募志愿者

    版权所有 Copyright@2009-2015豫ICP证合字09037080号

     纯自助论文投稿平台    E-mail:eshukan@163.com