Matlab contour plot smooth colors - matlab

Could you tell me how to plot in Matlab figure such as below (smooth transition of colors)? Function countour allows only to create plot with contour lines which doesn't provide enough information to me.

You can use imagesc with the 'jet' colormap. Here's an example:
x = conv2( randn(600), fspecial('gaussian',200,20), 'valid'); %// example 2D smooth data
imagesc(x)
colormap(jet)
colorbar
grid

That is not a contour plot!
try imagesc, surf and all of their variants:
http://uk.mathworks.com/help/matlab/surface-and-mesh-plots-1.html
http://uk.mathworks.com/help/matlab/image-file-operations.html

Related

Matlab isosurface plots lose 3d when used with subplot

I'm trying to plot a bunch of isosurface plots together with subplot, and I want them to appear 3d and be rotatable. However, I find that when I combine subplot with isosurface the plots appear 2d and are not rotatable. Here's a MRE:
[x,y,z] = meshgrid([-3:0.25:3]);
V = x.*exp(-x.^2 -y.^2 -z.^2);
for i=1:4
subplot(2,2,i);
isosurface(x,y,z,V,1e-4);
end
How can I make this rotatable? If I plot only one isosurface, without subplot, then it's in 3d and rotatable. If I use subplot with surf, I get 4 rotatable 3d surface plots. What am I missing here?
As explained here, subplot first splits your figure into different areas and creates a 2d axis object in one of the areas.
When you then isosurface after the call to subplot, MATLAB does a 2d projection of isosurface onto the 2d axis.
surf works differently, because it forces the conversion of the axis object where it is plotted to a 3d axis object. These behaviors can be recreated as follows:
figure;
plot(1,1)
surf([1,2;1,2],[1,1;2,2],[1 1 ; 1 1]);
figure
plot(1,1)
isosurface(x,y,z,V,1e-4);
A workaround for this is to force the conversion to a 3d Axis object with view(3)(plus re-adding camlight):
[x,y,z] = meshgrid([-3:0.25:3]);
V = x.*exp(-x.^2 -y.^2 -z.^2);
figure;
for ii = 1:4
subplot(2,2,ii);
isosurface(x,y,z,V,1e-4);
view(3)
camlight;
end

overlaying isolines on contourf plot

I would like to be able to overlay isolines over a filled contour or surface as it is done in this figure:
Can matlab overlay contour and contourf plots?
So far, I have tried this:
[X,Y] = meshgrid(x_cases,y_cases);
Points = length(x_cases)*length(y_cases);
resX = reshape(X,Points,1);
resY = reshape(Y,Points,1);
resZ = reshape(DataGrid_a,Points,1);
scatter(resX,resY,[],resZ,’filled’)
hold on
contour(X,Y,DataGrid_b,'ShowText','on')
But I have to reduce the transparency of my scatter plot to be able to see the contour lines from DataGrid_b, it would be more ideal to not change the transparency and overlay my isolines. I appreciate any input you may give me!
Thanks!
The simplest solution (and it's very hack) is to take advantage of the fact that 2D plots are plotted at Z = 0; So put your scatter points at some Z value below that.
scatter3(resX,resY,-ones(size(resX)),[],resZ,’filled’)
view(2)
hold on
contour(X,Y,DataGrid_b,'ShowText','on')

Matlab - Contour plot with labeled levels over Surf plot

I would like to plot a contour or contour3 with labeled levels over a surf plot with the same data using Matlab R2015b. Finally the figure is shown from above (the view in negative z-direction) to see the result.
My problem: The labels seem to disappear in the surf area - the product lacks the intended information.
My current test-code with the best result so far:
[X,Y,Z] = peaks;
v = -6:2:8;
hold on
surf(X,Y,Z)
shading interp
contour3(X,Y,Z,v,'k--','ShowText','on')
hold off
colormap default
caxis([-7,9])
view(0,90)
I am not yet allowed to post a picture of the result ..
Related questions in my consideration would be how to change contourf plots location on z-axis or shift the z-value of contour plot in Matlab 2014b to change the z-axis-property of a normal contour plot but they were no solution to my problem or did not work at all.
I finally understood your problem, you can solve it by doing all in 2D like so
[X,Y,Z] = peaks;
v = -6:2:8;
hold on
contourf(x,y,z,500,'LineStyle','none');
[C,h]=contour(x,y,z,v,'k--');
clabel(C,h,'FontSize',15);
caxis([-7,9])
view(0,90)

How to plot line s=(cost,sint,0) while 0<=t<=pi

I want to plot in matlab line s=(cost,sint,0) while 0<=t<=pi
t=0:.001:pi;
x=cos(t);
y=sin(t);
z=sin(t).*0;
mesh(x,y,z)
I get an error that z must be a matrix? how can I proceed?
You want plot3, not mesh. The function mesh is used for plotting surfaces in 3D. For plotting lines in 3D you need plot3:
t=0:.001:pi;
x=cos(t);
y=sin(t);
z=sin(t).*0;
plot3(x,y,z)
Note that z can be defined more simply as z=zeros(size(t)):
t=0:.001:pi;
x=cos(t);
y=sin(t);
z=zeros(size(t));
plot3(x,y,z)
Also, since z is zero in your case, you could use plot for drawing the line in 2D, and then use view for changing to the standard 3D view:
t=0:.001:pi;
x=cos(t);
y=sin(t);
plot(x,y)
view(3)
Use plot3(x, y, z).‏‏‏‏‏‏‏‏‏‏

Need help in 3D plot using MATLAB (X,Y,Z,V)

I need to ask help in plotting a 3D volume in MATLAB. My data set includes the X, Y, Z coordinate and corresponding intensity value, V.
I was using pcolor (X,Y,V) and shading interp while i was doing the 2D images but then I got stuck when I am about to create the 3D images. I have tried scatter3, smooth3 and slice but it seems it does not fit the function I need.
My goal is to plot the 3D grid with the corresponding intensity value per coordinate and apply shading interp between these points.
I am really new in 3D plotting and I would really appreciate any help in achieving my goal. Thank you so much!
Here are some example of images that I am trying to create
(source: ndt.net)
(source: www.bam.de)
I have a solution for your first example, in which you show three cross-sections of the volume data. With this solution you can essentially plot several pcolor with different orientations, in one same 3D figure.
First, you need the data for the different slices. This is exactly what you had when you did pcolor(X,Y,V) to plot the cross section in 2D, where X, Y and V are two dimensional matrices. You will also need to create a matrix Z with the z-position of the slice (e.g. Z = zeros(size(X)) + z0). Then you use the command surface(X,Y,Z,V) to plot the cross section image in 3D, where X,Y,Z are the positions, and V is the color (value of the function).
Repeat this procedure for all the other slices, using appropriate X,Y,Z,V matrices for each slice. For slices oriented on other axes you will have to define X,Y,Z accordingly. Remember to use "hold on" after the first surface command, so that all the slices are plotted.
Example:
surface(X1,Y1,Z1,V1); shading interp; hold on
surface(X2,Y2,Z2,V2); shading interp;
surface(X3,Y3,Z3,V3); shading interp;
colormap(jet(2048)); caxis([0,3]); % color axis colormap and scaling
axis equal; xlabel('X'); ylabel('Y'); zlabel('Z') % X,Y,Z scaling and label
Result figure here: