I want to create a countour plot such that my x axis and y axis are the coordinates of the plane and my z axis is height. I want to creat a 2b plot (contour) such that my height is differentiated with color. My z value has no relation with xy value other that the first value of z is corresponding to the cobination of first two values of x and y.
For example my x, y and z vectors are:
AAA=1:1:1000;
BBB=1:1:1000;
CCC=2:2:2000;

3 commentaires

DGM
DGM le 18 Mar 2022
if there's no correlation between Z and X,Y, then you don't have a meaningful description of a 2D dataset.
If the x,y,z vectors are correlated, but scattered, then use griddata() to create gridded data that's compatible with what contour() expects.
The correlation is that for each pair of x and y there is a corresponding value of z but the limitataion is that we only have one to one correspondance i.e., 1st value of x and first value of y corresponds to first value of z but first value of x and 2nd value of y corresponds to nothing.
DGM
DGM le 18 Mar 2022
Okay, yeah. That's just scattered data. Walter's suggestion looks pretty good.

Connectez-vous pour commenter.

 Réponse acceptée

You can always try just using griddata()
S = load('scattered.mat'); % load scattered data
n = 100;
x = linspace(min(S.x),max(S.x),n);
y = linspace(min(S.y),max(S.y),n);
[xg yg] = meshgrid(x,y); % create new grid
zg = griddata(S.x,S.y,S.z,xg,yg); % interpolate scattered data to grid
contour(xg,yg,zg) % now you can use contour(), contourf(), surf(), etc

8 commentaires

Let me explain my question again. I have 100 x values, 100 y values and 100 z values. Where x values represent x axis, y values represent y axis and z values represent z axis. Each z value correspond to pair of x and y value. Now I want to plot such that for different values of z I get different color. If z value is same e.g., 10 for 3 points then color should be same. I want to color code based on height. How can I do that ?
Are you sure you want an actual contour plot?
S = load('scattered.mat'); % load scattered data
n = 100;
x = linspace(min(S.x),max(S.x),n);
y = linspace(min(S.y),max(S.y),n);
[xg yg] = meshgrid(x,y); % create new grid
zg = griddata(S.x,S.y,S.z,xg,yg); % interpolate scattered data to grid
contourf(xg,yg,zg)
figure
pcolor(xg,yg,zg)
shading flat
Or maybe you want something else?
figure
S = load('scattered.mat'); % load scattered data
scatter3(S.x,S.y,S.z,10,S.z,'filled')
Actually the data has latitude(x) and longitude(y) of a road and also I have height of the road (z). Now I want to construct a plot which has shows height too and that height should be color coded.
I need something like this
You better share/attach the spatial data longitude A, lattitude B, elevation C (road height) here in the comments and edit your original post, so that you can get the proper help that you want.
You also need to confirm if the following conversion gives the geometry of the road.
x = (6371).*cos(B).*cos(A);
y = (6371).*cos(B).*sin(A);
scatter(x, y)
DGM
DGM le 21 Mar 2022
Modifié(e) : DGM le 21 Mar 2022
So you basically want variable-color lines. There are two basic ways to do that.
First, you could use a very dense scatter plot to create pseudolines. This is the simple way, and the data doesn't need to be ordered.
cmap = parula(64);
N = 1000; % how many points to plot?
x = linspace(0,10,N); % a straight line
y = linspace(0,10,N);
z = sin(linspace(-pi/2,7*pi/2,N)); % sinusoidally-varying z-height
h = scatter(x,y,10,z,'filled');
colormap(cmap);
colorbar
Alternatively, you can use a surf() plot. This will actually create lines, so the data needs to be ordered.
cmap = parula(64);
N = 100; % how many points to plot?
x = linspace(0,10,N); % a straight line
y = linspace(0,10,N);
z = sin(linspace(-pi/2,7*pi/2,N)); % sinusoidally-varying z-height
h = surf([x(:) x(:)],[y(:) y(:)],[z(:) z(:)]);
set(h,'facecolor','none','edgecolor','interp');
set(h,'linewidth',3); % make it fat so it's easier to demonstrate
view(2); % only show 2-D view
colormap(cmap);
colorbar
That said, I don't know how exactly this data is arranged -- how dense it is, etc. It may become complicated if the points are relatively sparse and unordered.
Sam Chak
Sam Chak le 21 Mar 2022
Hi @DGM
His data of the position of the road (in Spherical coordinate system) was posted here in the first comment of the first answer (by @KSSV).
DGM
DGM le 21 Mar 2022
Oh. Well it doesn't make much sense spending time and confusion here then. Thanks for the heads up.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 18 Mar 2022

0 votes

2 commentaires

Muhammad Qaisar Fahim
Muhammad Qaisar Fahim le 18 Mar 2022
Modifié(e) : Walter Roberson le 19 Mar 2022
Hi Walter,
Error using tricontour (line 58)
Incorrect input dimensions
Error in runMatlogOSM_2nodes (line 25)
tricontour(A,B,C,1)
Eventhough I have downloaded the function and the path as
addpath(genpath('./tricontour'), '-end');
My Command line is
tricontour(A,B,C,1)
The first parameter to tricontour() needs to be the xy coordinates, each row being an x y pair.
The second parameter to tricontour() needs to be the triangular connectivity data.
You might want to use https://www.mathworks.com/help/matlab/ref/delaunaytriangulation.html and extract the Points and ConnectivityList properties of the result.

Connectez-vous pour commenter.

Produits

Version

R2022a

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by