【问题标题】:MATLAB Simple Point PlotsMATLAB 简单点图
【发布时间】:2013-11-04 04:23:30
【问题描述】:

在 while 循环中,我需要绘制两个实体的位置 (x,y)。也就是说,我需要做的就是生成一个带有两个点的图。我需要将绘图缩放到特定的最大 x 和 y 值。另一个要求是,其中一个点需要在其周围放置三个同心环,每个同心环具有给定的半径。此外,这一切都发生在一个循环中,因此我希望只打开一个绘图窗口,并且我不会打开一大堆窗口(每个循环迭代一个)。

基本上这是我正在尝试(但失败了!)实现的伪代码:

-> Open new plot window, with a given x and y axis
while (running) {
  -> Clear the plot, so figure is nice and clean
  -> Plot the two points
  -> Plot the three circles around point A
}

我在 MATLAB 的文档中找到了几个项目,但似乎没有一个绘图函数可以满足我的要求,或者在某些情况下,我无意中创建了多个仅使用部分数据的绘图(即,一个绘图具有点,而另一个绘图具有圆圈)。

【问题讨论】:

    标签: matlab plot


    【解决方案1】:

    这是您可以在 while 循环中使用的示例代码

    x0=1; y0=4; 
    x1=2; y1=3;  % the x-y points
    r=[1 2 3]; % 3 radii of concentrating rings
    ang=0:0.01:2*pi; 
    xc=cos(ang)'*r;
    yc=sin(ang)'*r;
    
    plot(x0,y0,'.',x1,y1,'.'); % plot the point A
    hold on
    plot(x1+xc,y1+yc); % plot the 3 circles
    
    % set the limits of the plots (though Matlab does it for you already)
    xlim([min([x0 x1])-max(r) max([x0 x1])+max(r)]);
    ylim([min([y0 y1])-max(r) max([y0 y1])+max(r)]);
    
    hold off
    

    您可以很容易地循环完成这项工作,请阅读 matlab 的文档以了解如何做到这一点。

    【讨论】:

      【解决方案2】:

      试试这样的:

      r = [0.25 0.125 0.0625];
      d = (1:360) / 180 * pi;
      xy_circle = [cos(d)' sin(d)'];
      xy_circle_1 = r(1) * xy_circle;
      xy_circle_2 = r(2) * xy_circle;
      xy_circle_3 = r(3) * xy_circle;
      
      h_plot = plot(0, 0, '.k');
      hold on
      h_circle_1 = plot(xy_circle_1(:, 1), xy_circle_1(:, 2), '-b');
      h_circle_2 = plot(xy_circle_2(:, 1), xy_circle_2(:, 2), '-r');
      h_circle_3 = plot(xy_circle_3(:, 1), xy_circle_3(:, 2), '-g');
      axis equal
      
      for hh = 1:100
        xy = rand(2, 2) / 4 + 0.375;
        xlim = [0 1];
        ylim = [0 1];
        set(h_plot, 'XData', xy(:, 1));
        set(h_plot, 'YData', xy(:, 2));
      
        set(gca, 'XLim', xlim)
        set(gca, 'YLim', ylim)
      
        set(h_circle_1, 'XData', xy_circle_1(:, 1) + xy(1, 1));
        set(h_circle_1, 'YData', xy_circle_1(:, 2) + xy(1, 2));
        set(h_circle_2, 'XData', xy_circle_2(:, 1) + xy(1, 1));
        set(h_circle_2, 'YData', xy_circle_2(:, 2) + xy(1, 2));
        set(h_circle_3, 'XData', xy_circle_3(:, 1) + xy(1, 1));
        set(h_circle_3, 'YData', xy_circle_3(:, 2) + xy(1, 2));
      
        pause(1)
      end
      

      您可以随意更改参数。

      【讨论】:

        【解决方案3】:

        您可以使用以下功能

        figure;        %creates a figure
        hold on;       %overlays points and circles
        clf;           %clear the figure
        

        并为点和圆使用两种不同大小的标记(.o

        plot(x,y, 'b.', 'MarkerSize', 4);
        plot(x,y, 'ro', 'MarkerSize', 10);
        plot(x,y, 'go', 'MarkerSize', 14);
        plot(x,y, 'bo', 'MarkerSize', 18);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-02
          • 2013-08-17
          • 1970-01-01
          • 2011-01-08
          • 1970-01-01
          相关资源
          最近更新 更多