MATLAB:如何在使用“填充”功能时保持填充椭圆的宽高比?

问题描述 投票:1回答:1

我正在绘制一个x-y图上的实心圆和椭圆。省略号来自2x2张量数据。 x方向和y方向具有完全不同的单位,具体取决于我绘制的数据类型。我想在绘图上的某个(x,y)位置绘制椭圆,但我希望无论x轴和y轴单位如何都能保持绘制椭圆的纵横比。请注意,axis equal不是一个选项,因为x和y标度是如此不同。如果我尝试做axis equal它只是使情节非常小。

下面是一个简单的例子。在这个例子中,第一个椭圆总是一个完美的圆形,供参考,看看图形如何扭曲它。任何帮助表示赞赏。

x = 100*[1 2 3 4]; %x direction is several orders of magnitude larger than y direction
y = [1 2 3 4]; %y direction
data = randn(4,1); %data to fill ellipse (irrelevant to the question)

nrot = 36; %Number of points on each ellipse

for i = 1:4
    %Elements of 2x2 tensor to make the ellipse
    r1 = randn; r2 = randn; r3 = randn; r4 = randn;

    for irot=1:nrot %Loop to get ellipse points

        rot_ang = (irot-1)*360/(nrot-1);
        v = [cosd(rot_ang),sind(rot_ang)]; %Rotating vector components

        if i == 1 %Ensure that the first ellipse is a perfect circle
            r1 = 0; r4 = 0; r3 = r2;
        end

        plot_vec = [r1 r2; r3 r4]*v';

        %x component of ellipse to plot
        ex(irot) = plot_vec(1);

        %y component of ellipse to plot
        ey(irot) = plot_vec(2);  
    end 

    %Plot the ellipse at the x-y location
    xp = x(i)+ex;
    yp = y(i)+ey;

    fill(xp,yp,data(i)); hold on %Plot ellipses filled with "data".


end

%"Axis equal" does not work in this case
%axis equal
matlab matlab-figure axis fill ellipse
1个回答
2
投票

听起来您希望椭圆的显示数据宽高比为1:1,即使轴的数据宽高比不是这样。一种选择是首先为轴选择所需的data aspect ratio,然后在翻译和绘制它们之前相应地缩放椭圆的y值:

x = 100*[1 2 3 4];
y = [1 2 3 4];
aspectRatio = 100;  % Choose an aspect ratio of 100
data = randn(4, 1);
nrot = 36;

for i = 1:4

  r1 = randn; r2 = randn; r3 = randn; r4 = randn;

  for irot = 1:nrot

    rot_ang = (irot-1)*360/(nrot-1);
    v = [cosd(rot_ang), sind(rot_ang)];

    if i == 1
      r1 = 0; r4 = 0; r3 = r2;
    end

    plot_vec = [r1 r2; r3 r4]*v';
    ex(irot) = plot_vec(1);
    ey(irot) = plot_vec(2);

  end

  xp = x(i)+ex;
  yp = y(i)+ey./aspectRatio;  % Scale ellipse y data by aspect ratio

  fill(xp, yp, data(i));
  hold on;
  daspect(gca, [aspectRatio 1 1]);  % Set aspect ratio of axes

end

这是由此产生的情节:

enter image description here

椭圆很小,但如果你缩放,你会发现它们看起来具有正确的宽高比(即第一个看起来像一个圆圈)。

© www.soinside.com 2019 - 2024. All rights reserved.