# plotting concentric circles using contour

13 views (last 30 days)
Randy Chen on 26 Oct 2020 at 19:12
Commented: Star Strider on 27 Oct 2020 at 2:57
I'm trying to plot a few concentric circles with the following codes, but what I get is an ellipse, i don't know why:
[x,y] = meshgrid(-3:0.1:3,-3:0.1:3);
z = x.^2+y.^2;
contour(x,y,z)

Star Strider on 26 Oct 2020 at 20:10
Use the axis function, specifically either:
axis('equal') % Function Expression
or:
axis equal % Command Expression
.

Star Strider on 26 Oct 2020 at 22:01
That will be diffiicult in contour, although relatively straightforward with plot:
[x,y] = meshgrid(-3:0.1:3,-3:0.1:3);
z = x.^2+y.^2;
[cm,ch] = contour(x,y,z);
hold on
axis('equal')
Lvls = ch.LevelList;
LvlIdx = find(ismember(cm(1,:),Lvls));
LvlNrs = cm(2,LvlIdx);
centre = [mean(cm(1,2:LvlNrs(1))); mean(cm(2,2:LvlNrs(1)))];
angls = linspace(0, 2*pi, 33);
XL = get(gca,'XLim');
YL = get(gca,'Ylim');
plot([centre(1);max(XL(:))]*cos(angls), [centre(2);max(YL(:))]*sin(angls), '-g')
Experiment to get the result you want.
Randy Chen on 27 Oct 2020 at 2:26
thanks a lot!
Star Strider on 27 Oct 2020 at 2:57
As always, my pleasure!