I want to plot a surface plot of 2d data (rectangle)

7 vues (au cours des 30 derniers jours)
Muhammad Faheem Awan
Muhammad Faheem Awan le 31 Août 2018
Hi, I have x and y vectors, and corresponding values in vector z. I am also giving the vectors. consider x =[1:1:35]', y =[1:1:35]', z=[-55.4 -59.1 -57.4 -56.2 -59.1 -53.5 -56.1 -54.6 -55.2 -57.1 -53.0 -55.4502612542349 -55.4077517669503 -56.4923353520300 -55.9251080559888 -55.0880626240375 -56.5513629493757 -59.9456926093940 -60.0680208257504 -56.9962224622326 -58.2962763219373 -57.8927048783149 -60.3357449788936 -62.3074344511875 -60.7735542755245 -59.6553390538291 -58.8784323561426 -60.0117227083943 -61.5516366520366 -62.6449934321167 -60.6378148012631 -60.2066312113008 -60.5654267675394 -61.6400960109808 -63.0116960206390]. By scatter(x,y,10,z) I can plot the figure attached. Now I want to plot the surface of that data. If any one can help.
  2 commentaires
Rik
Rik le 31 Août 2018
You can check out the surf function
Muhammad Faheem Awan
Muhammad Faheem Awan le 31 Août 2018
I tried with surf, it then says z should be a matrix..

Connectez-vous pour commenter.

Réponse acceptée

John D'Errico
John D'Errico le 31 Août 2018
Modifié(e) : John D'Errico le 31 Août 2018
NO. You don't have a surface. You have a single set of points, apparently z(x,y). But here, ALL of your points lie along the line x==y. You have 35 values for x, 35 values for y, and 35 values for z. And at every one of those points, x==y.
So you have insufficient information to create a surface. Because all of your points lie in a straight line.
plot(y,z','-')
xlabel 'X,Y'
ylabel 'Z'
Sorry, but that does not yield a surface. There is NO additional information provided by x, beyond that which you provide in y (or vice versa.)
Now, the .png that you show is a completely different thing. It suggests that what you really want to do is reshape the VECTOR of z values into a 5x7 array.
So, really x and y are NOT the vectors 1:35. They might be vectors 1:5 or 1:7, or it seems they have other units.
You MIGHT start with
Zsurf = reshape(z,5,7);
Or, perhaps you want it as
Zsurf = reshape(z,7,5);
I cannot know which it is. I might guess the former. So I might do this:
Zsurf = reshape(z,5,7);
surf(Zsurf)
Better would be to then set the axes to the proper units. You can do that in the call to surf too.
Finally, it looks like that grid shown is not in fact a nice, regular meshed grid. But you've not told us what the true x and y values are that comprise the grid.
  4 commentaires
John D'Errico
John D'Errico le 3 Sep 2018
However, your data does NOT lie on a regular grid. We can plot it as a surface:
x = reshape(x,5,7);
y = reshape(y,5,7);
z = reshape(z,5,7);
surf(x,y,z)
view(0,90)
But as you can see, the points do not lie on some nice regular grid. So, while we can try to use a simple linear interpolation, as interp2 would provide, it will fail.
interp2(x,y,z,-50,-190)
Error using interp2>makegriddedinterp (line 237)
Input grid is not a valid MESHGRID.
Error in interp2 (line 136)
F = makegriddedinterp(X, Y, V, method,extrap);
That means you have a scattered data interpolation problem. Simplest is then:
si = scatteredInterpolant(x(:),y(:),z(:))
si =
scatteredInterpolant with properties:
Points: [35×2 double]
Values: [35×1 double]
Method: 'linear'
ExtrapolationMethod: 'linear'
si(-50,-190)
ans =
-58.456
Muhammad Faheem Awan
Muhammad Faheem Awan le 3 Sep 2018
@john, Thank You very much for your time. Highly appreciated. If I consider some random values for x and y, or normalize x and y by dividing on the highest number and consider it between 0 and 1. Then Can I use interp2?

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by