How to plot (x,y) points and their value, without meshgrid ?
Show older comments
Hello everybody, I am writing about a question on making a 2D plot with colors.
I have 3 arrays of points: x and y represents the positions of the points and l is "its value" I want to plot. They all have a size of 350x350 for example. This means that x(i,j) goes with y(i,j) with the corresponding l(i,j) value. I want to obtain a plot like the one below (a 3D plot would work as well). My problem is that my arrays x and y are not in a meshgrid form and the points are not evenly spread...

The only solution I found seems to take my points, create an interpolation and a meshgrid , and finally evaluate the meshgrid using the interpolation... Here is my code:
% Resize the arrays
X = reshape(abs(x), [], 1);
Y = reshape(abs(y), [], 1);
L = reshape(abs(l), [], 1);
% Create the interpolation
n_pts = 1000;
F = TriScatteredInterp(X,Y,L);
[yi,xi] = ndgrid(min(Y(:)):abs(max(Y(:)))/n_pts:max(Y(:)), ...
min(X(:)):abs(max(X(:)))/n_pts:max(X(:)));
vi = F(xi,yi)/max(L);
% Plot the result
plot_map = figure;
surf(xi, yi, vi, 'EdgeColor','None');
view(2);
xlabel('x')
ylabel('y')
At the end, the creation of my points is much faster than the plotting, which should be the inverse... And it doesn't do exactly what I want... Is there a possibility to obtain this kind of plot by showing exactly the points I obtained and not an interpolation ? Maybe is it possible to reshape my arrays to create a meshgrid ? In the case I have to make an interpolation, is their a faster way ?
I thank you in advance for your time and your answers !
Spyroth
1 Comment
Nader Rihan
on 3 Aug 2021
Edited: Nader Rihan
on 3 Aug 2021
Hello,
if you have 3 vectors (x,y,z) and you want to plot them as 3D or as contorplot lets do the following (for simplicity lets assume the lengths of x,y and z have square root values = L):
x_axis = reshape(x,L,L);
y_axis = reshape(y,L,L);
z_axis = reshape(z,L,L);
surf(x_axis,y_axis,z_axis)
OR
contourf(x_axis,y_axis,z_axis)
I read the accepted answer, but I found it limited, so the above works well, please accept it if you appriciate it.
Good luck.
Accepted Answer
More Answers (0)
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!