在绘图中的点周围绘制圆圈

时间:2016-03-11 13:43:22

标签: matlab plot border matlab-figure

我有两个矩阵

timeline =  [0.0008    0.0012    0.0016    0.0020    0.0024    0.0028];

Origdata =

   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轴

2 个答案:

答案 0 :(得分:5)

您可以使用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-radius"无论您的真实轴长宽比如何,它都显示为圆形,这样的事情实际上可以做到这一点。

以下代码的技巧是将数据和绘制宽高比(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

答案 1 :(得分:2)

要在MATLAB中绘制圆圈,您显然必须使用rectangle函数;)

正如我在评论中所提到的,0.3524的大小与您的轴不匹配,因此我选择了不同的尺寸以使圆圈实际可见,这些是rxry

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

enter image description here