Draw only the border of scatter plot in Matlab - matlab

I have a bunch of 2 dimensional vectors in Matlab. If I plot them in two dimension space using scatter they fill an area which is not convex. Is there ay way to simply draw only the border of the area? I have tried hist plus contour or convex hull but they do not do the job.

From matlab help:
x = gallery('uniformdata',[10,1],0);
y = gallery('uniformdata',[10,1],1);
DT = delaunayTriangulation(x,y);
k = convexHull(DT)
figure
plot(DT.Points(:,1),DT.Points(:,2), '.','markersize',10);
hold on
plot(DT.Points(k,1),DT.Points(k,2),'r')
hold off

Related

MATLAB - 3D volume fill based on inequalities

I have three variables x, y and z. I have inequalities of the form
x >= a, y>= b, z>=c, x+y>=d, y+z>=e, x+z>=f, x+y+z>=g
where a to g are positive numbers. On a 3D plot with axes x, y and z, this is an open volume. I would like to fill the open side (i.e. away from 0) shape with color and show it in a plot. What is the way to do this on MATLAB?
I attempted to use fill3 and a mesh but the result was not very good
[x,y,z] = meshgrid(0:0.01:2,0:0.01:2,0:0.01:2);
ineq = (x>=1)& (y>0.5)&(z>=0.25)&(x+y>1.25)&(y+z>0.6)&(x+z>1.1)&(x+y+z>1.6);
fill3(x(:),y(:),z(:), 'r')
box on
grid on
Using plot3 also was not very good. Is there any other way to generate a nice 3D figure on MATLAB?
Mathematica does this using RegionPlot3D. I was hoping for a similar resultant image.
First of all, be careful when using 3D meshes, the one you defined contains 8M+ points.
Assuming your shape is convex, you can use convhull and trisurf:
Not that the option 'Simplify' is set as true to reduce the number of elements accounted for in the convex hull.
[x,y,z] = meshgrid(0:0.1:2,0:0.1:2,0:0.1:2);
ineq = (x>=1)& (y>0.5)&(z>=0.25)&(x+y>1.25)&(y+z>0.6)&(x+z>1.1)&(x+y+z>1.6);
figure;
x_ineq = x(ineq);
y_ineq = y(ineq);
z_ineq = z(ineq);
id_cvhl = convhull(x_ineq,y_ineq,z_ineq,'Simplify',true);
trisurf(id_cvhl,x_ineq,y_ineq,z_ineq,'FaceColor','cyan','edgecolor','none')
xlim([0 2])
ylim([0 2])
zlim([0 2])
In case you want the result to look a bit more than RegionPlot3D, don't use Simplify, and plot the edges (Be careful not too have a mesh with too many points!).
id_cvhl = convhull(x_ineq,y_ineq,z_ineq);
trisurf(id_cvhl,x_ineq,y_ineq,z_ineq,'Facecolor','yellow')

Plotting the x-y plane projection of a 3d plot (Wigner function) along with the 3d plot in a single figure

I want a plot as the following:
Here, the Wigner function calculated from the x,y data is plotted as W and its projection on the x-y plane is also shown.
I have used the following code that plots the Wigner function. How to show the projection of W on the x-y plane in the same plot?
xvec = [-2:2];
yvec = xvec;
W = wfunc(psi,xvec,yvec,g);
f1 = figure(1);
mesh(xvec,yvec,real(W));
shading interp
You can use hold on to add many graphical objects in the same figure. For example:
figure
surf(xvec,yvec,real(W),'linestyle','none')
hold on
contourf(xvec,yvec,real(W),100,'linestyle','none')
You can shift the z-position of the contour colormap (following to the comment here) using
[~,hc] = contourf(xvec,yvec,real(W),100,'linestyle','none');
hcpatches = findobj(hc,'Type','Patch');
for n=1:length(hcpatches)
set(hcpatches(n),'ZData',-5*ones(size(get(hcpatches(n),'XData'))))
end
Also, you can try to use surfc.

How to plot a 3D surface with a circle in it?

I have a rational polynomial function. I find zeros of numerator and denominator of it. Now I want to draw this function and I do it with meshgrid and mesh command in matlab. How can I draw a circle in this shape? I add my result figure at first and second figure is an image that I want to be like that( draw red circle).
Create x and y for your circle:
r = 1;
theta = 0:0.1:2*pi;
x = r*cos(theta);
y = r*sin(theta);
Get the value of your function at the x and y's and plot a line in 3D with the values:
z = f(x,y);
plot3(x,y,z);
The final result may have some artefacts where the line crosses in and out of the surface. If you are not so concerned about the accuracy in the plot add a very small value to z to "lift" it above the surface.

Arc between two 3d vectors

How I can simply make an arc with a label in 3D Matlab plot? I have two 3D vectors (plot::Arrow3d)and I want to name an angle between them and I want to show it on 3D plot.
Edit1:
I use MuPad to render my drawing, I suppose to draw the arc between two vectors by plot::Arc3d(1, [0,0,0], n, al..bet). where n is simple to find. But I completely don't understand where the arc angle starts in 3D. Does somobody can show me how to find the zero angle.
Short answer, use the text function.
See if this gets you started:
%A couple of random points in 3 space
xyz1 = randn(3,1);
xyz2 = randn(3,1);
%Set up a figure, and create "arrow" plots within
figure(3781);
clf;
hold on
quiver3(0,0,0,xyz1(1), xyz1(2), xyz1(3),0,'b')
quiver3(0,0,0,xyz2(1), xyz2(2), xyz2(3),0,'r')
view(3)
%Add a line connecting teh arrows, and a text label
plot3([xyz1(1) xyz2(1)], [xyz1(2) xyz2(2)], [xyz1(3) xyz2(3)],'k:')
xyzCenter = mean([xyz1 xyz2],2);
h = text(xyzCenter(1), xyzCenter(2), xyzCenter(3), 'Label text here');
set(h,'Color','b')
get(h); %For more properties to set

Surface plot for square matrix and labelling data points

I want to plot 3D surface plots (which look like mountains). My data are square matrices. I want to be able to label the data where there is kind of a big cliff.
How can I go about it? Thanks
For a single 2D matrix you can use SURF function to plot 3D surface:
% generate random square 2D matrix 20x20
x = rand(20);
% make some (10) mountains
x(randi(numel(x),10,1))=rand(10,1)+5;
% plot surface
surf(x)
How do you want to plot multiple surfaces? On a single figure?
To label the large points lets threshold the data:
cutvalue = 1;
iHigh = find(x(:) > cutvalue);
[irow,icol] = ind2sub(size(x), iHigh);
hold on
plot3(icol, irow, x(iHigh), 'ro')
hold off