如何在MATLAB绘图中显示图形的缩放部分?

如何在MATLAB绘图中显示图形的缩放部分?,matlab,matplotlib,plot,matlab-figure,Matlab,Matplotlib,Plot,Matlab Figure,我在Matlab绘图上有大约四个系列的数据,其中两个非常接近,只能通过缩放来区分。如何在现有绘图中为查看器描绘缩放的部分。我查过类似的帖子,但答案似乎很不清楚 我在寻找这样的东西: 以下是如何使用MATLAB实现这一点的建议。它可能需要一些微调,但会给您带来以下结果: function pan = zoomin(ax,areaToMagnify,panPosition) % AX is a handle to the axes to magnify % AREATOMAGNIFY is the

我在Matlab绘图上有大约四个系列的数据,其中两个非常接近,只能通过缩放来区分。如何在现有绘图中为查看器描绘缩放的部分。我查过类似的帖子,但答案似乎很不清楚

我在寻找这样的东西:


以下是如何使用MATLAB实现这一点的建议。它可能需要一些微调,但会给您带来以下结果:

function pan = zoomin(ax,areaToMagnify,panPosition)
% AX is a handle to the axes to magnify
% AREATOMAGNIFY is the area to magnify, given by a 4-element vector that defines the
%      lower-left and upper-right corners of a rectangle [x1 y1 x2 y2]
% PANPOSTION is the position of the magnifying pan in the figure, defined by
%        the normalized units of the figure [x y w h]
%

fig = ax.Parent;
pan = copyobj(ax,fig);
pan.Position = panPosition;
pan.XLim = areaToMagnify([1 3]);
pan.YLim = areaToMagnify([2 4]);
pan.XTick = [];
pan.YTick = [];
rectangle(ax,'Position',...
    [areaToMagnify(1:2) areaToMagnify(3:4)-areaToMagnify(1:2)])
xy = ax2annot(ax,areaToMagnify([1 4;3 2]));
annotation(fig,'line',[xy(1,1) panPosition(1)],...
    [xy(1,2) panPosition(2)+panPosition(4)],'Color','k')
annotation(fig,'line',[xy(2,1) panPosition(1)+panPosition(3)],...
    [xy(2,2) panPosition(2)],'Color','k')
end

function anxy = ax2annot(ax,xy)
% This function converts the axis unites to the figure normalized unites
% AX is a handle to the figure
% XY is a n-by-2 matrix, where the first column is the x values and the
% second is the y values
% ANXY is a matrix in the same size of XY, but with all the values
% converted to normalized units

pos = ax.Position;
%   white area * ((value - axis min) / axis length)   + gray area
normx = pos(3)*((xy(:,1)-ax.XLim(1))./range(ax.XLim))+ pos(1);
normy = pos(4)*((xy(:,2)-ax.YLim(1))./range(ax.YLim))+ pos(2);
anxy = [normx normy];
end
请注意,要放大的
区域的单位类似于轴单位,而
panPosition
的单位介于0到1之间,类似于MATLAB中的
position
属性

以下是一个例子:

x = -5:0.1:5;
subplot(3,3,[4 5 7 8])
plot(x,cos(x-2),x,sin(x),x,-x-0.5,x,0.1.*x+0.1)
ax = gca;
area = [-0.4 -0.4 0.25 0.25];
inlarge = subplot(3,3,3);
panpos = inlarge.Position;
delete(inlarge);
inlarge = zoomin(ax,area,panpos);
title(inlarge,'Zoom in')

以下是如何使用MATLAB实现这一点的建议。它可能需要一些微调,但会给您带来以下结果:

function pan = zoomin(ax,areaToMagnify,panPosition)
% AX is a handle to the axes to magnify
% AREATOMAGNIFY is the area to magnify, given by a 4-element vector that defines the
%      lower-left and upper-right corners of a rectangle [x1 y1 x2 y2]
% PANPOSTION is the position of the magnifying pan in the figure, defined by
%        the normalized units of the figure [x y w h]
%

fig = ax.Parent;
pan = copyobj(ax,fig);
pan.Position = panPosition;
pan.XLim = areaToMagnify([1 3]);
pan.YLim = areaToMagnify([2 4]);
pan.XTick = [];
pan.YTick = [];
rectangle(ax,'Position',...
    [areaToMagnify(1:2) areaToMagnify(3:4)-areaToMagnify(1:2)])
xy = ax2annot(ax,areaToMagnify([1 4;3 2]));
annotation(fig,'line',[xy(1,1) panPosition(1)],...
    [xy(1,2) panPosition(2)+panPosition(4)],'Color','k')
annotation(fig,'line',[xy(2,1) panPosition(1)+panPosition(3)],...
    [xy(2,2) panPosition(2)],'Color','k')
end

function anxy = ax2annot(ax,xy)
% This function converts the axis unites to the figure normalized unites
% AX is a handle to the figure
% XY is a n-by-2 matrix, where the first column is the x values and the
% second is the y values
% ANXY is a matrix in the same size of XY, but with all the values
% converted to normalized units

pos = ax.Position;
%   white area * ((value - axis min) / axis length)   + gray area
normx = pos(3)*((xy(:,1)-ax.XLim(1))./range(ax.XLim))+ pos(1);
normy = pos(4)*((xy(:,2)-ax.YLim(1))./range(ax.YLim))+ pos(2);
anxy = [normx normy];
end
请注意,要放大的
区域的单位类似于轴单位,而
panPosition
的单位介于0到1之间,类似于MATLAB中的
position
属性

以下是一个例子:

x = -5:0.1:5;
subplot(3,3,[4 5 7 8])
plot(x,cos(x-2),x,sin(x),x,-x-0.5,x,0.1.*x+0.1)
ax = gca;
area = [-0.4 -0.4 0.25 0.25];
inlarge = subplot(3,3,3);
panpos = inlarge.Position;
delete(inlarge);
inlarge = zoomin(ax,area,panpos);
title(inlarge,'Zoom in')

?我建议你添加一些图片来展示你想要实现的目标。我不清楚你到底在问什么以及你的问题在哪里。你可以设置x轴和y轴的限制,但可以添加一些问题的图像使用
tikz
:P你可能可以在MATLAB中执行类似的操作,但它不是rigth工具?我建议你添加一些图片来展示你想要实现的目标。我不清楚你到底在问什么以及你的问题在哪里。你可以设置x轴和y轴的限制,但可以添加一些问题的图像使用
tikz
:P你可能可以在MATLAB中执行类似的操作,但这不是rigth工具极力推荐的答案!如何删除连接绘图和“放大”的线条?只需从功能中删除此部分:
注释(图,'line',[xy(1,1)panPosition(1)],…[xy(1,2)panPosition(2)+panPosition(4)],'Color','k')注释(图,'line',[xy(2,1)panPosition(1)+panPosition(3)],…[xy(2,2)panPosition 2],'Color','k')
如何使用您的功能缩放绘图的两部分?强烈建议回答!如何删除连接绘图和“放大”的线条?只需从功能中删除此部分:
注释(图,'line',[xy(1,1)panPosition(1)],…[xy(1,2)panPosition(2)+panPosition(4)],'Color','k')注释(图,'line',[xy(2,1)panPosition(1)+panPosition(3)],…[xy(2,2)panPosition 2],'Color','k')
如何使用函数缩放绘图的两部分?