matlab - 围绕绘图中的点绘制圆圈

标签 matlab plot border matlab-figure

我有两个矩阵

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, '.');

enter image description here

如何在每个点周围画一个半径为 0.3524 的圆?此半径应相对于仅 y 轴

最佳答案

您可以使用 viscircles 轻松完成此操作(这需要 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

enter image description here

它看起来像这样的原因是因为你的 X 数据相对于你的 y 数据非常靠在一起,为了使圆显示为圆,我强制轴的 x 和 y 缩放相等(axis equal)

编辑

如果您只想让radius 相对于y 轴(距离),那么我们实际上需要绘制具有x 和y 半径的椭圆。我们想要缩放“x 半径”以使其显示为一个圆,而不管您的真实轴纵横比如何,这样的事情实际上可以做到这一点。

下面代码的技巧是将数据和绘图纵横比(pbaspectdaspect)设置为手动。这确保了轴的纵横比在缩放、调整大小等过程中不会改变,并确保我们的“圆圈”保持圆形外观。

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

enter image description here

只是为了演示,如果我们将圆半径增加到 4.3524,我们可以更好地看到圆。

enter image description here

这适用于所有调整大小等。

enter image description here

关于matlab - 围绕绘图中的点绘制圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35941827/

相关文章:

python - 将多个文本文件中具有一定行数的一列数据拉入一个文本文件

python - Python 散点图颜色点

python - 使用 matplotlib 中的数据框绘制 3D 图

python - Pandas Plot,如何控制条形宽度和间隙

将Matlab/C函数转换为无源代码版本

Matlab XTickLabels 警告

matlab - 如何将矩阵的行连接成一个向量?

java - Java中隐藏透明JScrollPane的边框

CSS:创建凸起框效果的好方法是什么?

c# - Winforms - 如何创建自定义窗口边框和关闭/最小化按钮?