在绘图中给点画圆

3

我有两个矩阵

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轴


如果你画一个该半径的圆,它比你的x轴要大得多,并且远远超出范围。你的x轴上的值在0.0008和0.00028之间。 - Daniel
我知道这不是你要求的,但如果你的圆仅与y轴相关,就像我现在理解的那样,我会使用条形图代替:errorbar(repmat(timeline.',1,size(Orgidata,2)),Orgidata,0.3524*ones(size(Orgidata)),'.') - Daniel
2个回答

5
你可以轻松使用 viscircles 实现这一点(需要 图像处理工具箱),但我认为输出实际上并不是你所期望的。
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

这样看起来的原因是,相对于y数据,您的X数据非常接近,为了让圆形呈现出圆形,我强制设置了轴的x和y缩放比例相等(axis equal)。如果您只想让半径相对于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


谢谢答案。如何避免这个问题。可能是不使用axis equal。 - Mayank Lakhani
@MayankLakhani 问题出在你的数据上,因为轴缩放的原因,正如Daniel和我都提到的那样。我已经添加了一个替代我的解决方案。 - Suever
1
@MayankLakhani 一定要试试我刚发布的迭代,它在缩放、平移、调整大小等操作时能更好地保证圆形保持圆形。 - Suever
1
警告:viscircles需要图像处理工具箱。 - rayryeng
2
那个 GIF 真的需要这么让人头晕吗?:-P - Luis Mendo
显示剩余2条评论

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


嗨,丹尼尔,谢谢你的回答。正如我已经提到的,我需要一个半径为0.3524的圆。实际上,y位置是距离,x是时间。 - Mayank Lakhani
1
我看到了,但是这些大小和你的轴不符。你整个x轴只有0.002的长度,那么这样大小的圆形怎么能适合你的图表呢?你可以轻松地输入你的值,我相信那不是你期望的结果。 - Daniel
我想仅定义这个门(边界)在y方向上。那么我该如何做呢? - Mayank Lakhani
1
好的,现在我明白了。您可以使用另一个答案中解释的解决方案:ry=0.3524; rx=ry/yrng*xrng; - Daniel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接