I have a 2D intensity plot, as in this example:
[xx yy] = meshgrid(0:0.1:1, 0:0.1:1);
figure(1)
imagesc(sin(xx)) %(x,y)-plot at z=0
Now, as I have noted in the comment, this plot is in the xy-plane and I have taken z=0. I'd like to plot this in a 3D coordinate system as a function of x and y, but taking z=0. I tried with plot3 in this way, plot3(xx,yy,cos(yy)), but that only plots lines and gives them a curvature which I am not interested in. I'd like only a plane at z=0.
You can use surf to accomplish this. We use the xx and yy meshgrid outputs as the x and y positisions, a matrix of zeros as the z value and sin(xx) as the color. We also remove the edges by setting the EdgeColor to none.
surf(xx, yy, zeros(size(xx)), sin(xx), 'EdgeColor', 'none')
Related
I have a set of n=8000 cartesian coordinates X,Y and Z as vectors and also a vector V of same size which I want to use as values to create a heatmap on a sphere.
I saw this link (visualization of scattered data over a sphere surface MATLAB), but I don't understand how I convert this set of data into a meshgrid for plotting using surf.
Almost every example I saw uses meshgrids.
Right now, I am doing by plotting a sphere and then use scatter3 to plot my points as big balls and try to smooth them later. I looks like this:
I would like to get the figure as the plotting of the example in that link, where he uses:
k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
c = hadamard(2^k);
surf(x,y,z,c);
colormap([1 1 0; 0 1 1])
axis equal
EDIT:
(Sorry for taking so long to reply, the corona crises kept away from work)
What I am actually doing is:
for i=1:numel(pop0n)
ori(i,:)=ori(i,:)/norm(ori(i,:));
end
x = ori(:,1);
y = ori(:,2);
z = ori(:,3);
%// plot
m=100;
[aa,bb,cc] = sphere(m);
surf(aa,bb,cc,ones(m+1,m+1)*min(pop0n))
hold on
colormap jet;
scatter3(x,y,z,400,pop0n/norm(pop0n),'filled');
colorbar
shading interp
The array 'ori' is 8000x3, and contains the x, y and z coordinates of the points I want to plot and pop0n is a 8000 sized vector with the intensities of each coordinate.
My main question is how do I transform my x, y, z and pop0n, that are vectors, into 2D arrays (meshgrid) to use surf?
Because I cannot simply do surf(x,y,z,pop0n) if they are vectors.
Thanks in advance
As David suggested, griddata does the job.
What I did was:
for i=1:numel(pop0n)
ori(i,:)=ori(i,:)/norm(ori(i,:));
end
x = ori(:,1);
y = ori(:,2);
z = ori(:,3);
%// plot
m=100;
[aa,bb,cc] = sphere(m);
v = griddata(x,y,z,pop0n,aa,bb,cc,'nearest');
surf(aa,bb,cc,v)
colormap jet;
colorbar
shading interp
Suppose I have a function w from 2D coordinate space to 1D values, such that
w(a,b) := min(a,b)
I want to represent this function as an heat map in Matlab.
For example, I draw a square on the Cartesian plane with vertices (0,0), (0,1), (1,0), (0,0). Then I colour the region inside the square, by assigning the same colour to any two pairs of points (a,b) and (c,d) if w(a,b)=w(c,d).
Is my understanding of a heatmap correct? If so, how can I achieve this in Matlab?
You can achieve this with surf, which plots a surface which looks like a heatmap when viewed from above. See the commented code for details:
% Define axes points as [0,0.1,0.2,...,1]
ax = 0:0.1:1;
% Make grid of values corresponding to min(x,y)
xy = bsxfun(#min, ax, ax');
% Plot
surf(xy); % Plot surface
view(2); % Look from "above" onto x-y plane
set(gca, 'XTickLabel', ax) % Set x-axis labels
set(gca, 'YTickLabel', ax) % Set y-axis labels
colorbar; % Show colorbar as a legend
Output:
You can extend this by using a custom function if min was just an example.
ax = 0:0.1:1;
% Define function
w = #(a,b) a - exp(b)
% Use custom function inside bsxfun
xy = bsxfun(w, ax, ax');
surf(xy); % ... etc
See the docs for details: https://uk.mathworks.com/help/matlab/ref/bsxfun.html
Also see the docs for pcolor which creates a 2D colour plot so no perspective change is needed, and contourf which outlines regions not all squares.
I am trying to plot a very simple function in the 3d plane.
f=zeros(101,101);
xs=0:0.1:10;
ys=0:0.1:10;
for j=1:101
f(1,j)=ys(j);
end
Here are 3 plots:
The first is a plot of f vs x at ymin:
figure; plot(xs,f(:,1),'*r')
xlabel('x')
ylabel('f')
The second is a plot of f vs y at xmin:
figure; plot(ys,f(1,:),'*r')
xlabel('y')
ylabel('f')
And finally the third is a 3d mesh:
figure; mesh(xs,ys,f)
xlabel('x')
ylabel('y')
However the mesh plot seems to contradict the 2 2d plots, it seems to have the x and y switched around if you get me. Can anyone help? Should it be mesh(ys,xs,f) for some reason? Thanks!
mesh didn't switch your x and y, it's a matter of definition. Don't forget that a matrix doesn't have an x axis or a y axis but rather a row dimension and a column dimension. The row dimension is normally stated first in a pair just like the x dimension but if you are equating it too an image then you would normally have the x axis going across which is actually along the column axis!
Try like this rather
for j=1:101
f(j,1)=ys(j);
end
figure; plot(xs,f(1,:),'*r')
xlabel('x')
ylabel('f')
figure; plot(ys,f(:,1),'*r')
xlabel('y')
ylabel('f')
figure; mesh(xs,ys,f)
xlabel('x')
ylabel('y')
I am using plot3 to plot a matrix such that the resultant figure shows different colors for each vector of that matrix:
plot3(Grid.x1(:,:),Grid.x2(:,:),phi(:,:))
How can I smooth the coloring of this plot? Thanks!
You can use varycolor from FileExchange to construct and control a continuous range color spectrum. This way the transition between different lines will seem more natural and proximal lines will have similar colors.
You can read more and see examples usage here http://blogs.mathworks.com/pick/2008/08/15/colors-for-your-multi-line-plots/
Example:
[X,Y,Z] = peaks(128);
% raw plot3()
figure; plot3(X, Y, Z);
% define set equal to line number
ColorSet = varycolor(128);
% smooth plot3()
figure; hold all;
set(gca, 'ColorOrder', ColorSet);
plot3(X, Y, Z); view(3);
Update:
For a continuous 3D plot (i.e. a surface) you can use surfl instead of plot3 and display your data as a 3-D Surface Plot (with Shading). You can additionally apply any colormap on the resulting surface, i.e. gray or ColorSet as above.
surfl(X,Y,Z);
shading interp;
colormap(gray);
I've got the following code which works for plotting an x-y slice of the Hydrogen 2pz orbital:
%probability densities
pd_psi_210 = #(r,theta,phi) exp(-r).*(r.^2).*(cos(theta).^2)/(32*pi);
%configuring the range
[x y z] = meshgrid(-10:.1:10,-10:.1:10,-2:.1:2);
[THETA,PHI,R] = cart2sph(x,y,z);
%create array of probability density magnitudes
psi_210_vals = pd_psi_210(R,THETA,PHI);
%plotting
imagesc(psi_210_vals(:,:,1)); %x-y plane
I'd like to plot the 3d contour plot of the orbital. I've tried this (and it doesn't seem to get me what I wanted):
isosurface(psi_210_vals(:,:,:)); %3D contour
How can I get this to work?
You just have to specify the underlying grid, and the level you want. For example:
>> isosurface(-10:.1:10, -10:.1:10, -2:.1:2, psi_210_vals, 0.001);
>> axis equal