围绕绘图中的点绘制圆圈
Drawing circles around points in a plot
我有两个矩阵
timeline = [0.0008 0.0012 0.0016 0.0020 0.0024 0.0028];
原始数据 =
79.8400 69.9390 50.0410 55.5082 34.5200 37.4486 31.4237 27.3532 23.2860 19.3039
79.7600 69.8193 49.8822 55.3115 34.2800 37.1730 31.1044 26.9942 22.8876 18.9061
79.6800 69.6996 49.7233 55.1148 34.0400 36.8975 30.7850 26.6352 22.4891 18.5084
79.6000 69.5799 49.5645 54.9181 33.8000 36.6221 30.4657 26.2762 22.0907 18.1108
79.5200 69.4602 49.4057 54.7215 33.5600 36.3467 30.1464 25.9173 21.6924 17.7133
79.4400 69.3405 49.2469 54.5249 33.3200 36.0714 29.8271 25.5584 21.2941 17.3159
当我绘制它们时,我得到如下图。
plot(timeline, Origdata, '.');
如何在每个点周围画一个半径为 0.3524 的圆?此半径应仅相对于 y 轴。
您可以使用 viscircles
(which requires the Image Processing Toolbox) 轻松完成此操作,但我认为输出实际上不是您所期望的。
radius = 0.3524;
dots = plot(timeline, Origdata, '.');
hold on
for k = 1:numel(dots)
plotdata = get(dots(k));
centers = [plotdata.XData(:), plotdata.YData(:)];
% Ensure the the colors match the original plot
color = get(dots(k), 'Color');
viscircles(centers, radius * ones(size(centers(:,1))), 'Color', color);
end
它看起来像这样的原因是因为你的 X 数据相对于你的 y 数据 非常 靠得很近,为了让圆圈显示为圆圈,我强制 x 和轴的 y 缩放比例相等 (axis equal
)
编辑
如果您只希望 radius
相对于 y 轴(距离),那么我们实际上需要绘制具有 x 和 y 半径的椭圆。我们想缩放 "x-radius" 使其显示为圆形,而不管您的真实轴纵横比如何,这样的事情实际上可以做到这一点。
下面代码的技巧是将数据和绘图纵横比(pbaspect
和 daspect
)设置为手动。这确保了轴的纵横比在缩放、调整大小等过程中不会改变,并确保我们的 "circles" 保持圆形外观。
dots = plot(timeline, Origdata, '.');
drawnow
% Force the aspect ratio to not change (keep the circles, as circles)
pbaspect('manual')
daspect('manual')
hold on
aspectRatio = daspect;
t = linspace(0, 2*pi, 100);
t(end+1) = NaN;
radius = 4.3524;
% Scale the radii for each axis
yradius = radius;
xradius = radius * aspectRatio(1)/aspectRatio(2);
% Create a circle "template" with a trailing NaN to disconnect consecutive circles
t = linspace(0, 2*pi, 100);
t(end+1) = NaN;
circle = [xradius*cos(t(:)), yradius*sin(t(:))];
for k = 1:numel(dots)
x = get(dots(k), 'XData');
y = get(dots(k), 'YData');
color = get(dots(k), 'Color');
% Center circle template at all points
circles = arrayfun(@(x,y)bsxfun(@plus, [x,y], circle), x, y, 'uni', 0);
circles = cat(1, circles{:});
plot(circles(:,1), circles(:,2), 'Color', color)
end
只是为了演示,如果我们将圆半径增加到 4.3524,我们可以更好地看到圆。
这适用于所有调整大小等
要在 MATLAB 中画圆,显然必须使用 rectangle
函数 ;)
如我的评论所述,0.3524
的大小与您的轴不匹配,因此我选择了不同的大小以使圆圈实际可见,这些是 rx
和 ry
timeline = [0.0008 0.0012 0.0016 0.0020 0.0024 0.0028];
Orgidata =[79.8400 69.9390 50.0410 55.5082 34.5200 37.4486 31.4237 27.3532 23.2860 19.3039
79.7600 69.8193 49.8822 55.3115 34.2800 37.1730 31.1044 26.9942 22.8876 18.9061
79.6800 69.6996 49.7233 55.1148 34.0400 36.8975 30.7850 26.6352 22.4891 18.5084
79.6000 69.5799 49.5645 54.9181 33.8000 36.6221 30.4657 26.2762 22.0907 18.1108
79.5200 69.4602 49.4057 54.7215 33.5600 36.3467 30.1464 25.9173 21.6924 17.7133
79.4400 69.3405 49.2469 54.5249 33.3200 36.0714 29.8271 25.5584 21.2941 17.3159];
ry=1;
rx=0.0001;
dots=plot(timeline, Orgidata , '.');
hold on
for ix=1:size(Orgidata ,1)
for jx=1:size(Orgidata ,2)
rectangle('Position',[timeline(ix)-(rx/2),Orgidata(ix,jx)-(ry/2),rx,ry],'Curvature',[1,1],'EdgeColor',get(dots(jx), 'Color'));
end
end
我有两个矩阵
timeline = [0.0008 0.0012 0.0016 0.0020 0.0024 0.0028];
原始数据 =
79.8400 69.9390 50.0410 55.5082 34.5200 37.4486 31.4237 27.3532 23.2860 19.3039
79.7600 69.8193 49.8822 55.3115 34.2800 37.1730 31.1044 26.9942 22.8876 18.9061
79.6800 69.6996 49.7233 55.1148 34.0400 36.8975 30.7850 26.6352 22.4891 18.5084
79.6000 69.5799 49.5645 54.9181 33.8000 36.6221 30.4657 26.2762 22.0907 18.1108
79.5200 69.4602 49.4057 54.7215 33.5600 36.3467 30.1464 25.9173 21.6924 17.7133
79.4400 69.3405 49.2469 54.5249 33.3200 36.0714 29.8271 25.5584 21.2941 17.3159
当我绘制它们时,我得到如下图。
plot(timeline, Origdata, '.');
如何在每个点周围画一个半径为 0.3524 的圆?此半径应仅相对于 y 轴。
您可以使用 viscircles
(which requires the Image Processing Toolbox) 轻松完成此操作,但我认为输出实际上不是您所期望的。
radius = 0.3524;
dots = plot(timeline, Origdata, '.');
hold on
for k = 1:numel(dots)
plotdata = get(dots(k));
centers = [plotdata.XData(:), plotdata.YData(:)];
% Ensure the the colors match the original plot
color = get(dots(k), 'Color');
viscircles(centers, radius * ones(size(centers(:,1))), 'Color', color);
end
它看起来像这样的原因是因为你的 X 数据相对于你的 y 数据 非常 靠得很近,为了让圆圈显示为圆圈,我强制 x 和轴的 y 缩放比例相等 (axis equal
)
编辑
如果您只希望 radius
相对于 y 轴(距离),那么我们实际上需要绘制具有 x 和 y 半径的椭圆。我们想缩放 "x-radius" 使其显示为圆形,而不管您的真实轴纵横比如何,这样的事情实际上可以做到这一点。
下面代码的技巧是将数据和绘图纵横比(pbaspect
和 daspect
)设置为手动。这确保了轴的纵横比在缩放、调整大小等过程中不会改变,并确保我们的 "circles" 保持圆形外观。
dots = plot(timeline, Origdata, '.');
drawnow
% Force the aspect ratio to not change (keep the circles, as circles)
pbaspect('manual')
daspect('manual')
hold on
aspectRatio = daspect;
t = linspace(0, 2*pi, 100);
t(end+1) = NaN;
radius = 4.3524;
% Scale the radii for each axis
yradius = radius;
xradius = radius * aspectRatio(1)/aspectRatio(2);
% Create a circle "template" with a trailing NaN to disconnect consecutive circles
t = linspace(0, 2*pi, 100);
t(end+1) = NaN;
circle = [xradius*cos(t(:)), yradius*sin(t(:))];
for k = 1:numel(dots)
x = get(dots(k), 'XData');
y = get(dots(k), 'YData');
color = get(dots(k), 'Color');
% Center circle template at all points
circles = arrayfun(@(x,y)bsxfun(@plus, [x,y], circle), x, y, 'uni', 0);
circles = cat(1, circles{:});
plot(circles(:,1), circles(:,2), 'Color', color)
end
只是为了演示,如果我们将圆半径增加到 4.3524,我们可以更好地看到圆。
这适用于所有调整大小等
要在 MATLAB 中画圆,显然必须使用 rectangle
函数 ;)
如我的评论所述,0.3524
的大小与您的轴不匹配,因此我选择了不同的大小以使圆圈实际可见,这些是 rx
和 ry
timeline = [0.0008 0.0012 0.0016 0.0020 0.0024 0.0028];
Orgidata =[79.8400 69.9390 50.0410 55.5082 34.5200 37.4486 31.4237 27.3532 23.2860 19.3039
79.7600 69.8193 49.8822 55.3115 34.2800 37.1730 31.1044 26.9942 22.8876 18.9061
79.6800 69.6996 49.7233 55.1148 34.0400 36.8975 30.7850 26.6352 22.4891 18.5084
79.6000 69.5799 49.5645 54.9181 33.8000 36.6221 30.4657 26.2762 22.0907 18.1108
79.5200 69.4602 49.4057 54.7215 33.5600 36.3467 30.1464 25.9173 21.6924 17.7133
79.4400 69.3405 49.2469 54.5249 33.3200 36.0714 29.8271 25.5584 21.2941 17.3159];
ry=1;
rx=0.0001;
dots=plot(timeline, Orgidata , '.');
hold on
for ix=1:size(Orgidata ,1)
for jx=1:size(Orgidata ,2)
rectangle('Position',[timeline(ix)-(rx/2),Orgidata(ix,jx)-(ry/2),rx,ry],'Curvature',[1,1],'EdgeColor',get(dots(jx), 'Color'));
end
end