Matlab - Contour plot with labeled levels over Surf plot - matlab

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)

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 smooth colors

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

MATLAB - Plotting a smooth volume from 3D scatter plot data

So I have data in the form [x y z intensity] that I plot on a scatter3 figure with xyz axes. The colour of the data is used to dictate the intensity value. Problem is, using a scatter plot means the data points show up as discrete points. What I need, is a smooth shape - so I guess I need some kind of interpolation between the points?
I've tried using trisurf, but the problem with this one is that it interpolates between points that it shouldn't. So where I should have 'gaps' in my surface, it joins up the edges instead so it fills in the gaps. See the attached pics for clarification.
Does anyone have any suggestions?
The code I use is as below (the commented out scatter3 is what does the scatter plot, the rest does the trisurf):
% Read in data
dataM = csvread('3dDispersion.csv');
% scatter3(dataM(:,1), dataM(:,2), dataM(:,3), 5, dataM(:,4),'filled');
% Plot
hold on;
x = dataM(:,1);
y = dataM(:,2);
freq = dataM(:,3);
tri = delaunay(x,y);
h = trisurf(tri, x, y, freq);
% Make it pretty
% view(-45,30);
view(3);
axis vis3d;
lighting phong;
shading interp;
Use the boundary function in Matlab. This will apply a mesh similar to shrinkwrap over your points. In order to reduce the "gap closers", you will want to increase the "shrink factor".
Try K = boundary(X,Y,Z,0.9)
Where X, Y & Z are the vectors of your data points
https://www.mathworks.com/help/matlab/ref/boundary.html
You can then use trimesh or related surface plotting functions depending on how you want to display it.

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: