如何绘制一个圆圈?

时间:2015-12-28 12:45:58

标签: matlab plot scatter-plot

以下代码在Matlab中绘制圆圈。如何在其中指定MarkerEdgeColorMarkerFaceColor

function plot_model
exit_agents=csvread('C:\Users\sony\Desktop\latest_mixed_crowds\December\exit_agents.csv');
%scatter(exit_agents(:,2),exit_agents(:,3),pi*.25^2,'filled');
for ii =1:size(exit_agents,1),
    circle(exit_agents(ii,2),exit_agents(ii,3),0.25);
end
end
function h = circle(x,y,r)
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
end

使用绘图和散射在缩放时奇怪地缩放它们。这不是我想要的。

1 个答案:

答案 0 :(得分:6)

various options来绘制圆圈。最简单的是,实际绘制具有全曲率的filled rectangle

%// radius
r = 2;

%// center
c = [3 3];

pos = [c-r 2*r 2*r];
r = rectangle('Position',pos,'Curvature',[1 1], 'FaceColor', 'red', 'Edgecolor','none')
axis equal

使用 R2014b 更新图形引擎,这非常顺利:

enter image description here

如果你有一个旧版本的Matlab而不是 R2014b ,你需要坚持使用三角学方法,但是使用fill来填充它:

%// radius
r = 2;
%// center
c = [3 3];
%// number of points
n = 1000;
%// running variable
t = linspace(0,2*pi,n);

x = c(1) + r*sin(t);
y = c(2) + r*cos(t);

%// draw filled polygon
fill(x,y,[1,1,1],'FaceColor','red','EdgeColor','none')
axis equal

enter image description here

“分辨率”可以通过点数n自由缩放。

您的功能可能看起来像

function h = circle(x,y,r,MarkerFaceColor,MarkerEdgeColor)
hold on
c = [x y];
pos = [c-r 2*r 2*r];
r = rectangle('Position',pos,'Curvature',[1 1], ...
    'FaceColor', MarkerFaceColor, 'Edgecolor',MarkerEdgeColor)
hold off
end