meshgrid plotting how to speficy x-axis and y-axis values?

12 views (last 30 days)
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
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
Jeff Eriksen on 14 Oct 2016
Thanks for the additional info. I am more clear now on what each of these do now. I almost always use imagesc to plot a 2D matrix, whether it is correlations, a photograph, or just about anything. Very rarely do I wish to use surf. Now I realize surf works he way I want in this context since I pass it X and Y explicitly. For imagesc and contour, they rely on the indices and have no knowledge of the underlying (x,y) position of the matrix. So now my questions is refined as follows: how can I get imagesc and contour (the latter especially) to display the x,y position on the axes instead of the matric index numbers?
Star Strider
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.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!