MATLAB简单点图
在while循环中,我需要绘制两个实体的位置(x,y)。也就是说,我需要做的就是生成一个有两点的图。我需要将绘图缩放到特定的最大x和y值。另外一个要求是,其中一个点需要有三个同心环围绕它放置,每个同心环具有给定的半径。此外,这一切都是在一个循环中发生的,因此我希望只打开一个绘图窗口,而不是打开一系列窗口(每个循环迭代一个窗口) 基本上,以下是我正在尝试(但失败!)实现的伪代码:MATLAB简单点图,matlab,plot,Matlab,Plot,在while循环中,我需要绘制两个实体的位置(x,y)。也就是说,我需要做的就是生成一个有两点的图。我需要将绘图缩放到特定的最大x和y值。另外一个要求是,其中一个点需要有三个同心环围绕它放置,每个同心环具有给定的半径。此外,这一切都是在一个循环中发生的,因此我希望只打开一个绘图窗口,而不是打开一系列窗口(每个循环迭代一个窗口) 基本上,以下是我正在尝试(但失败!)实现的伪代码: -> Open new plot window, with a given x and y axis while
-> 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的文档中找到了几项,但似乎没有一个绘图函数可以满足我的要求,或者有一些例子是我无意中创建了多个绘图,其中只有一些数据(即,一个绘图有点,另一个有圆)。以下是一个示例代码,您可以在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文档。尝试以下方法:
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
您可以根据需要更改参数。您可以使用以下功能
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);