How to plot a filled circle?

有些话、适合烂在心里 提交于 2019-12-13 16:17:29

问题


The below code plots circles in Matlab. How can I specify the MarkerEdgeColor and MarkerFaceColor in it.

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

Using plot and scatter scales them weirdly when zooming. This is not what I wish for.


回答1:


There are various options to plot circles. The easiest is, to actually plot a filled rectangle with full curvature:

%// 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

With the update of the graphics engine with R2014b this is really smooth:

If you have an older version of Matlab than R2014b, you will need to stick with your trigonometric approach, but use fill to get it filled:

%// 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

The "resolution" can be freely scaled by the number of points n.


Your function could then look like

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


来源:https://stackoverflow.com/questions/34493798/how-to-plot-a-filled-circle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!