meshgrid plotting how to speficy x-axis and y-axis values?
Show older comments
I create a meshgrid and calculate a function camel. When I plot it, it displays indices, not X and Y values on the plot axes. How can I make it show the X and Y values on the plot axes instead?
[X,Y] = meshgrid(-3:0.1:3); camel = (4-2.1*X.^2+X.^4/3).*X.^2+X.*Y+4*(Y.^2-1).*Y.^2; imagesc(camel)
Answers (1)
Star Strider
on 14 Oct 2016
I’m not certain what you want to do.
Try this:
[X,Y] = meshgrid(-3:0.1:3);
camel = (4-2.1*X.^2+X.^4/3).*X.^2+X.*Y+4*(Y.^2-1).*Y.^2;
figure(1)
surf(X,Y,camel)
grid on
4 Comments
Jeff Eriksen
on 14 Oct 2016
Star Strider
on 14 Oct 2016
I don’t understand your using those functions to plot it. If you want to restrict the axes to those limits and look at it from the top down, this works:
[X,Y] = meshgrid(-3:0.1:3);
camel = (4-2.1*X.^2+X.^4/3).*X.^2+X.*Y+4*(Y.^2-1).*Y.^2;
figure(1)
surf(X,Y,camel)
grid on
axis tight
view([0, 90])
If you wnat it without the grid lines, this works:
[X,Y] = meshgrid(-3:0.1:3);
camel = (4-2.1*X.^2+X.^4/3).*X.^2+X.*Y+4*(Y.^2-1).*Y.^2;
figure(1)
hs = surf(X,Y,camel);
grid on
axis tight
view([0, 90])
set(hs, 'LineStyle','none')
If you want a square plot, replace:
axis tight
with:
axis equal
Jeff Eriksen
on 14 Oct 2016
Star Strider
on 14 Oct 2016
My pleasure.
The transition of my code to use contour (or here contourf) is straightforward:
[X,Y] = meshgrid(-3:0.1:3);
camel = (4-2.1*X.^2+X.^4/3).*X.^2+X.*Y+4*(Y.^2-1).*Y.^2;
figure(1)
[C,hs] = contourf(X,Y,camel);
grid on
axis equal
view([0, 90])
set(hs, 'LineStyle','none')
To use imagesc requires a couple tweaks:
x = -3:0.1:3;
y = x;
figure(2)
imagesc(x,y,camel)
axis image
Experiment with the resolution of the meshgrid argument and ‘x’ (use the linspace function) to vary the resolution easily.
Categories
Find more on Line Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!