Improve surface plot visualisation of scatter points - matlab

I want to visualize 4 vectors of scattered data with a surface plot. 3 vectors should be the coordinates. In addition the 4th vector should represent a surface color.
My first approach was to plot this data (xk,yk,zk,ck) using
scatHand = scatter3(xk,yk,zk,'*');
set(scatHand, 'CData', ck);
caxis([min(ck), max(ck)])
As a result I get scattered points of different color. As these points lie on the surface of a hemisphere it ist possible to get colored faces instead of just points. I replace the scattered points by a surface using griddata to first build an approximation
xk2=sort(unique(xk));
yk2=sort(unique(yk));
[xxk, yyk]=meshgrid(xk2, yk2);
zzk=griddata(xk,yk,zk,xxk,yyk,'cubic');
cck=griddata(xk,yk,clr,xxk,yyk,'cubic');
surf(xxk,yyk,zzk,cck);
shading flat;
This is already nearly what I want except that the bottom of the hemisphere is ragged. Of course if I increase the interpolation point numbers it gets better but than the handling of the plot gets also slow. So I wonder if there is an easy way to force the interpolation function to do a clear break. In addition it seems that the ragged border is because the value of zzk gets 'NaN' outside the circle the hemisphere shares with the z=0-plane.
The red points at the top are the first several entries of the original scattered data.

You can set the ZLim option to slice the plotted values within a certain range.
set(gca, 'Zlim', [min_value max_value])

Related

impoly only approximately correct on log-scale axes

When determining points within polygons using MATLAB's inpolygon function, I find that the results are exactly correct for polygons drawn on linear axes but only approximately correct for polygons drawn on log-scale axes. Although my suspicions lean in favor of a MATLAB bug, it's possible I've overlooked something.
The following code reproduces the issue I have been experiencing with other data. The results are shown in the following image (the bottom set of panels are zoomed views of the top panels). One can appreciate that there are unlabeled points inside the polygon and labeled points outside the polygon, neither of which should occur, in the case of a polygon drawn on log-scale axes (right). In contrast, the polygon test is exact for polygons drawn on linear axes (left).
n=2E4;
x(:,1)=rand(n,1); y(:,1)=rand(n,1);
x(:,2)=lognrnd(0.5,0.25,n,1); y(:,2)=lognrnd(0.5,0.25,n,1);
for m=1:2
subplot(1,2,m);
scatter(x(:,m),y(:,m),'.'); hold on;
if(m==2)
set(gca,'xscale','log'); set(gca,'yscale','log');
end
p=impoly(gca);
pc=getPosition(p);
in=inpolygon(x(:,m),y(:,m),pc(:,1),pc(:,2));
scatter(x(in,m),y(in,m),20);
end
I think you missed something: A line in normal scale is not a line in log scale. Your polygons are not properly drawn in the log scale, as you draw 2 points and put them together with a straight line.
Look at the real polygon in log space:
close all
clear
n=2e4;
x(:,1)=rand(n,1); y(:,1)=rand(n,1);
x(:,2)=lognrnd(0.5,0.25,n,1); y(:,2)=lognrnd(0.5,0.25,n,1);
for m=1:2
subplot(1,2,m);
scatter(x(:,m),y(:,m),'.'); hold on;
if(m==2)
set(gca,'xscale','log'); set(gca,'yscale','log');
end
p=impoly(gca);
pc=getPosition(p);
% plot polygon
hold on
for ii=1:size(pc,1)-1
plot(linspace(pc(ii,1),pc(ii+1,1),100),linspace(pc(ii,2),pc(ii+1,2),100),'g')
end
plot(linspace(pc(end,1),pc(1,1),100),linspace(pc(end,2),pc(1,2),100),'g')
in=inpolygon(x(:,m),y(:,m),pc(:,1),pc(:,2));
scatter(x(in,m),y(in,m),20);
end
Look at this zoomed in result (click to enlarge):
This happens because the polygon is defined in euclidean space, and it is defined as points linked by lines. If you want to work in log space, things may get complicated. One way to numerically approximate it is the inverse of what I did for plotting. Create dense enough sampled straight line on log space, convert it to linear space, and define a high vertex polygon with the resulting points. Then use inpolygon.

Plot lots of points with alpha value in Matlab

When you want to plot scatter points with fixed alpha value in Matlab, you may the patch function, like advised in this SO question. But when you want to plot a high number of individual points, you should use the plot function, as advised in this SO question.
Is it still possible to plot a high number of scatter points with fixed alpha value in Matlab ?

Plot 3D surface that is not the graph of a function

I have a 3D data set of a surface that is not a function graph. The data is just a bunch of points in 3D, and the only thing I could think of was to try scatter3 in Matlab. Surf will not work since the surface is not a function graph.
Using scatter3 gave a not so ideal result since there is no perspective/shading of any sort.
Any thoughts? It does not have to be Matlab, but that is my go-to source for plotting.
To get an idea of the type of surface I have, consider the four images:
The first is a 3D contour plot, the second is a slice in a plane {z = 1.8} of the contour. My goal is to pick up all the red areas. I have a method to do this for each slice {z = k}. This is the 3rd plot, and I like what I see here a lot.
Iterating this over z give will give a surface, which is the 4th plot, which is a bit noisy (though I have ideas to reduce the noise...). If I plot just the black surface using scatter3 without the contour all I get is a black indistinguishable blob, but for every slice I get a smooth curve, and I have noticed that the curves vary pretty smoothly when I adjust z.
Some fine-tuning will give a much better 4th plot, but still, even if I get the 4th plot to have no noise at all, the result using scatter3 will be a black incomprehensible blob when plotted alone and not on top of the 3D contour. I would like to get a nice picture of the full surface that is not plotted on top of the 3D contour plot
In fact, just to compare and show how bad scatter3 is for surfaces, even if you had exact points on a sphere and used scatter3 the result would be a black blob, and wouldn't even look like a sphere
Can POV-Ray handle this? I've never used it...
If you have a triangulation of your points, you could consider using the trisurf function. I have used that before to generate closed surfaces that have no boundary (such as polyhedra and spheres). The downside is that you have to generate a triangulation of your points. This may not be ideal to your needs but it definitely an option.
EDIT: As #High Performance Mark suggests, you could try using delaunay to generate a triangulation in Matlab
just wanted to follow up on this question. A quick nice way to do this in Matlab is the following:
Consider the function d(x, y, z) defined as the minimum distance from (x, y, z) to your data set. Make sure d(x, y, z) is defined on some grid that contains the data set you're trying to plot.
Then use isosurface to plot a (some) countour(s) of d(x, y, z). For me plotting the contour 0.1 of d(x, y ,z) was enough: Matlab will plot a nice looking surface of all points within a distance 0.1 of the data set with good lighting and all.
In povray, a blob object could be used to display a very dense collection of points, if you make them centers of spheres.
http://www.povray.org/documentation/view/3.6.1/71/
If you want to be able to make slices of "space" and have them colored as per your data, then maybe the object pattern (based on a #declared blob object) might do the trick.
Povray also has a way to work with df3 files, which I've never worked with, but this user appears to have done something similar to your visualization.
http://paulbourke.net/miscellaneous/df3/

plot a set of 3D data in different angles in MATLAB

I have a formula that depends on theta and phi (spherical coordinates 0<=theta<=2*pi and 0<=phi<=pi). By inserting each engle, I obtained a quantity. Now I have a set of data for different angles and I need to plot the surface. My data is a 180*360 matrix, so I am not sure if I can use SURF or MESH or PLOT3. The figure should be a surface that include all data and the axes should be in terms of the quantity, not the quantity versus the angles. How can I plot such a surface?
I see no reason why you cannot use mesh or surf to plot such data. Another option I tend to use is that of density plots. You basically display the dependent variable (quantity) as an image and include the independent variables (angles) along the axis, much like you would with the aforementioned 3D plotting functions. This can be done with imagesc.
Typically you would want your axes to be the dependent variables. Could you elaborate more on this point?
If I understand you correctly you have calculated a function f(theta,phi) and now you want to plot the surface containing all the points with the polar coordinated (r,theta,phi) where r=f(theta,phi).
If this is what you want to do, the 2D version of such a plot is included in MATLAB under the name polar. Unfortunately, as you pointed out, polar3 on MatlabCentral is not the generalization you are looking for.
I have been able to plot a sphere with the following code, using constant r=1. You can give it a try with your function:
phi1=0:1/(3*pi):pi; %# this would be your 180 points
theta1=-pi:1/(3*pi):pi; % your 360 points
r=ones(numel(theta1),numel(phi1));
[phi,theta]=meshgrid(phi1,theta1);
x=r.*sin(theta).*cos(phi);
y=r.*sin(theta).*sin(phi);
z=r.*cos(theta);
tri=delaunay(x(:),y(:),z(:));
trisurf(tri,x,y,z);
From my tests it seems that delaunay also includes a lot of triangles which go through the volume of my sphere, so it seems this is not optimal. So maybe you can have a look at fill3 and construct the triangles it draws itself: as a first approximation, you could have the points [x(n,m) x(n+1,m) x(n,m+1)] combined into one triangle, and [x(n+1,m) x(n+1,m+1) x(n+1,m+1)] into another...?

turn scatter plot into area plot

I have a 2D scatter plot in MATLAB. Is it possible to interpolate the scatter plot to create an area plot?
If you're simply trying to draw one large filled polygon around your entire set of scattered points, you can use the function CONVHULL to find the convex hull containing your points and the function PATCH to display the convex hull:
x = rand(1,20); %# 20 random x values
y = rand(1,20); %# 20 random y values
hullPoints = convhull(x,y); %# Find the points defining the convex hull
patch(x(hullPoints),y(hullPoints),'r'); %# Plot the convex hull in red
hold on; %# Add to the existing plot
scatter(x,y); %# Plot your scattered points (for comparison)
And here's the resulting figure:
Scatter is generally used to represent data where you can't use a line graph, i.e., where each x might have many different y values, so you can't convert directly to an area graph--it would be meaningless. If your data actually is representable as a line graph, then pass it to area directly.
So I'm not quite sure what you want, but here are some possibilities:
You could create a Voronoi diagram based on your points. This will show a region near your points showing which points are closer to a specific point: voronoi(x,y), or see the help.
You could bucket or quantize your data somehow, making it fit into a grid, and then plot the grid. This could also be considered a histogram, so read up on that.
You could just use larger scatter markers (scatter(x,y,scale) where scale is the same dimensions as x and y).