Plot clean surface plots from scatter data

13 views (last 30 days)
I'm trying to make some nice 3D plots of some surfaces being generated by my MATLAB program. I can't use surf() because I'm working in spherical coordinates, and thus each data point must be preserved as a triplet of points as one can't just convert from spherical triplets to the rectangular (ix1) x vector, (jx1) y vector, (ixj) z matrix that surf requires. Simply doing a 3D scatter plot is messy looking. I'd like a surface.
Now, I have already used the method detailed here: http://www.mathworks.com/matlabcentral/fileexchange/5105
But I have two problems with it.
1) The figures are hideous. The surfaces I'm plotting resemble slices off of a sphere. Peel a piece of onion skin off and that may help visualize the thin, curved shape I'm trying to make. I believe what is happening is the routine is trying to connect the points on the empty edges together, making a solid, but that ruins the figure. I also think the process of converting from spherical to cartesian coordinates has left me with unevenly spaced points which throws off the size of the triangles, contributing to the messy look.
2) I'd like the surface to be a solid color. It looks confusing with a color gradient, like I'm trying to add in extra data and meaning to what is simply a figure of a shape.
Does anyone know of a different way to plot a surface in this manner, or even better, a way to get MATLAB to handle spherical coordinates so I can plot my data directly and skip this step?
  2 Comments
Sean de Wolski
Sean de Wolski on 27 Jan 2012
Could you please post an image and/or sample set of data to a free webhosting site?
http://www.mathworks.com/matlabcentral/answers/7924-where-can-i-upload-images-and-files-for-use-on-matlab-answers
B K
B K on 27 Jan 2012
Apologies, hopefully this clarifies the issue:
Image 1: Done with just a scatterplot, you can see the way the surface is supposed to look but it's not ideal:
http://imgur.com/edkTm,2AMvN#0
Image 2: What I have so far using the aforementioned link, with the "exterior points connecting" issue visible:
http://imgur.com/edkTm,2AMvN#1

Sign in to comment.

Accepted Answer

Kelly Kearney
Kelly Kearney on 27 Jan 2012
I'm not quite clear on the format of your data to start, since you say you can't convert from spherical to cartesian in your case. Keep in mind that surf doesn't require a rectilinear grid, so if you have gridded data in spherical coordinates (assuming I understand your data description, if your triplets don't lie on a grid, you should be able to triagulate rho onto a uniform theta/phi grid), you can just translate and plot:
n = 20;
theta = (-n:2:n)/n*pi;
phi = (-n:2:n)'/n*pi/2;
[theta,phi] = meshgrid(theta,phi);
rho = rand(size(theta)) + 5;
[x,y,z] = sph2cart(theta, phi, rho);
surf(x,y,z, ones(size(x)))
  3 Comments
B K
B K on 29 Jan 2012
Thanks so much for that! Using meshgrid in that way solved my problem. If I may trouble you for one more thing, I am trying to plot multiple surfaces on a single plot so my user can see how the change in a variable in my code affects the plot shape. What I have now is basically:
figure(fignum)
res = 50;
theta = linspace(1.7, 2.6, res);
phi = linspace(-.1, 0.8, res);
[theta, phi] = meshgrid(theta, phi);
hold on
for c = 1:10
%Calculate r values based on model parameters
[x, y, z] = sph2cart(theta, phi, r);
surf(x,y,z,(ones(size(x))*c))
end
hold off
legend('1','2','3','4','5','6','7','8','9','10')
But all this does is give me the tenth shape, instead of plotting all 10 on the same figure. Any idea why this may be?
Kelly Kearney
Kelly Kearney on 30 Jan 2012
Is r supposed to change between iterations of c? As typed above, you're just replotting the same surface on top of itself 10 times (though you change the color each time).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!