Drawing circles around points in a plot

余生长醉 提交于 2019-12-12 12:27:13

问题


I have two matrices

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

When I plot them, I get a graph like below.

plot(timeline, Origdata, '.');

How can I draw a circle of radius 0.3524 value around each point? This radius should be relative to the y-axis only.


回答1:


You can do this easily using viscircles (which requires the Image Processing Toolbox) however I don't think that the output is actually what you're expecting.

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

The reason that it looks like this is because your X data is very close together relative to your y data and for circles to appear as circles, I have forced the x and y scaling of the axes to be equal (axis equal)

Edit

If you only want the radius to be relative to the y axis (distance) then we actually need to draw ellipses with an x and y radius. We want to scale the "x-radius" to make it appear as a circle regardless of your true axes aspect ratio, something like this can actually do that.

The trick to the code below is setting the data and plot aspect ratios (pbaspect and daspect) to manual. This ensures that the aspect ratio of the axes doesn't change during zoom, resizing, etc and makes sure that our "circles" remain circular-looking.

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

Just to demonstrate, if we increase the circle radius to 4.3524 we can see the circles better.

And this works with all resizing etc.




回答2:


To draw circles in MATLAB, you obviously have to use the rectangle function ;)

As mentioned in my comment, the size of 0.3524 does not match your axis, so I chose different sizes to have the circles actually visible, These are rx and 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



来源:https://stackoverflow.com/questions/35941827/drawing-circles-around-points-in-a-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!