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

25 次查看(过去 30 天)
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)

回答(1 个)

Star Strider
Star Strider 2016-10-14
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 个评论
Jeff Eriksen
Jeff Eriksen 2016-10-14
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 2016-10-14
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.

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by