Overlay the data points which make-up a contour plot matrix on the same plot in MATLAB - matlab

Hope the title gave an adequate description of my problem. Basically, I am generating a contour plot in MATLAB using the contourf (x,y,z) function, where x and y are vectors of different lengths and z is a matrix of data with dimensions of x times y. The contourf plot is fine, however, I am looking to overlay this plot with the actual data points from the matrix z. I have tried using the scatter function, but I am getting an error message informing me that X and Y must be vectors of the same length - which they're not. Is there any other way to achieve this?
Thanks in advance for any help/suggestions!

I think meshgrid should help you.
z = peaks; %// example 49x49 z data
x = 1:20;
y = 1:49;
z = z(y,x); %// make dimensions not equal so length(x)~=length(y)
[c,h] = contourf(x,y,z);
clabel(c,h); colorbar;
[xx,yy]=meshgrid(x,y); %// this is what you need
hold on;
plot(xx,yy,'k.'); %// overlay points on contourf
Notice plot suffices instead of scatter. If you insist, scatter(xx(:),yy(:),10), for example, does the trick. Although my example isn't particularly interesting, this should hopefully get you started toward whatever you're going for aesthetically.

Related

Missing row/column when plotting a surface in MATLAB

Have a look at the following MATLAB code and the resulting surface plot. Maybe I am doing a stupid mistake, but there is actually a row and column missing. The variable z is a 10x10 matrix, but the plot shows only 9x9 elements. How to plot the whole 10x10 matrix?
z = randn(10,10);
t = 1:10;
x = 1:10;
figure;
surf(t,x,abs(z),'EdgeColor','none');
axis xy; axis tight; colormap(jet); view(0,90);
I think this is a misunderstanding about what surf does, i.e. what a surface plot is:
What you seem to be wanting is an actual image instead of a surface plot, where for the former pixels correspond to the underlying values. What you get with surf is a graphical representation of lines at a certain height (abs(z) in your case), i.e. between your desired image pixels. Note that there are 10x10 lines in your 9x9 plot.
What you want can be achieved visually e.g. by:
z = randn(10,10);
t = 1:10;
x = 1:10;
figure
imshow(abs(z),[]),
axis on, colormap(gca,jet)
colorbar
hope this helps!

How to make a heat map of grid data

I have this data with values on the edges of the matrix and other values at evenly spaced interval within the matrix. I want to predict the values of the zero positions from the original values and make a heat map of the new data. Through suggest, I use scatteredInterpolant, ndgrid and interpolant since the data is that interp2 (matlab functions) cannot be used to interpolate the zero elements. Now, this method doe not give me a smooth figure and I am want to know if someone can offer some help. I have attached the figure from my code, the data and the code to this post.Thank you.
[knownrows, knowncolumns, knownvalues] = find(DataGrid); %get location and value of all non-zero points
interpolant = scatteredInterpolant(knownrows, knowncolumns, knownvalues,'linear'); %create interpolant from known values
[queryrows, querycolumns] = ndgrid(1:1:size(DataGrid, 1), 1:1:size(DataGrid, 2)); %create grid of query points
interpolatedj = interpolant(queryrows, querycolumns);
HeatMap(interpolatedj)
https://www.mediafire.com/?pq40x1ljxk8h996
https://www.mediafire.com/?pq40x1ljxk8h996
To plot a smoothed matrix you can use pcolor and set the shading parameter to interp
pcolor(M); %where M is your 2D matrix
shading interp %set the shading to interp
Try
image(M) or imagesc(M) where M is a matrix. pcolor(M) also works. If your matrix is huge then you need to remove edges otherwise figure just looks like blank image.

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.

Plotting points while plotting vectors : Matlab

I need to make a plot with only points and tried something like
plot(x,y)
where x and y are vectors: collection of points.
I do not want matlab to connect these points itself. I want to plot as if plotted with
for loop
plot;hold on;
end
I tried
plot(x,y,'.');
But this gave me too thick points.
I do not want to use forloop because it is time expensive. It takes a lot of time.
You're almost there, just change the MarkerSize property:
plot(x,y,'.','MarkerSize',1)
Try:
plot(x,y,'*');
or
plot(x,y,'+');
You can take a look to the documentation: http://www.mathworks.nl/help/matlab/creating_plots/using-high-level-plotting-functions.html
help scatter
IIRC: where S is the size of the scatter points:
scatter(x,y,S)
You may try this piece of code that avoid using loops. The plot created does not have lines but markers of different colors corresponding to each column of matrices x and y.
%some data (matrix)
x = repmat((2:10)',1,6);
y = bsxfun(#times, x, 1:6);
set(0,'DefaultAxesColorOrder', jet(6)); %set the default matlab color
figure('Color','w');
plot(x,y,'p'); %single call to plot
axis([1 11 0 70]);
box off;
legend(('a':'f')');
This gives

3D plot matlab with 3 equally sized vectors

I have three vectors, X, Y, and Z. All of equal length (20000,1). I want to plot all three in a 3d plot. I have tried using surf and plot3 but to no avail as they require Z to be of size (20000,20000). Can anybody help?
TIA
X = DAT(3,:);
Y = DAT(4,:);
Z = DAT(11,:);
[x,y] = meshgrid(X,Y);
surf(x,y,Z);
Have you tried griddata or TriScatteredInterp to create an interpolated surface?
NO! plot3 does NOT require that of Z. If all you wish is to plot a point set, then plot3 does EXACTLY what you want.
plot3(X,Y,Z,'.')
The point is, there is NO need to use meshgrid for plot3. In fact, there is no need to use meshgrid as you have tried in order to use surf. (If you will be calling griddata, then meshgrid would be necessary, but for a SMALLER mesh.)
IF you need a surface plot, then you need to create a surface. If the points are scattered, then your basic options are tools like triscatteredinter, griddata, or gridfit, the last from the file exchange.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/311767 gives the code snippet may help you.
X=rand(1,30);
Y=rand(1,30);
Z=rand(1,30);
[XI YI ZI] = griddata(X,Y,Z,linspace(0,1),linspace(0,1)');
figure
subplot(1,2,1)
trisurf(delaunay(X,Y),X,Y,Z)
subplot(1,2,2);
surf(XI,YI,ZI)