I have 3 vectors of data: latitudes, longitudes, and elevations of specific locations. Each vector (lat, lon, elev) is a column vector with about 63 elements where element 1 of each represents a given location. What I want to do is create a topological map, or heatmap (whichever you want to call it) to map out these locations. I can plot them (like lon vs lat) no problem, but I'm not sure where to look to create a topological map. I've looked at using the surf function, but the elevations need to be a 2-D matrix for that and that would result in a lot of missing data that I just don't have.
Can someone give me some guidance here?
You can use TriScatteredInterp to interpolate your data onto a regular grid, which you then can use to plot the surface using surf, or a heatmap using contourf.
The example in the linked documentation of TriScatteredInterp will do exactly what you need, all you need to do is replace x, y, and z by your data and define the appropriate limits for meshgrid.
Related
Suppose we have quiver field i.e. we have a meshgrid and then we assign a vector to each point. Is it possible to plot only the quiver field within some polygon?
So in the figure below, we want everything outside the triangle to be cropped out.
Ideally the code will also be helpful for the next step of having multiple such polygons and cropping out everything on their complement.
Some approaches:
A direct way is to figure out the meshgrid for the particular polygon and then assign a vector to each point. But that will take a lot of time to figure out as polygons get more complicated. In other words, the regular meshgrid is the square polygon, so we must modify the meshgrid matrix depending on our polygon. A friend informed me of a mesh generator matlab code.
Use inpolygon. The input of inpolygon are points in (x,y). But in our case we only have the vector field assigned to a meshgrid. One idea is to solve the ode system to obtain concrete solution pairs (x,y) to plug into the polygon. But solving them takes a lot longer and the pictures are not as nice.
Here's some sample code which I think will generate the kind of plot you want. It uses inpolygon to "filter" out the points inside the polygon. The vector field is still evaluated at the original meshgrid points. It easily extends to multiple polygons too.
clear
clc
x = linspace(0, 1, 21);
[X,Y] = meshgrid(x,x);
U = -Y; %some velocity field
V = X;
hold off
quiver(X,Y,U,V); %quiver on all points
polygon = [0.2,0.2;
0.7,0.5;
0.5,0.8]; %polygon vertices
ind = inpolygon(X,Y,polygon(:,1),polygon(:,2)); %get indices of points inside polygon
hold on
quiver(X(ind),Y(ind),U(ind),V(ind)); %quiver of points inside polygon
In one histc, I have latitude data. In another histc, I have longitude data. I have plotted the bar graphs of these 2 separately. Now, I want to combine them and produce a 3D graph where the x-axis is latitude, y-axis is longitude, and z-axis is the frequency with which each latitude-longitude pair occurs.
The problem is that, while plotting the graphs for latitudes and longitudes separately, I calculated their respective frequencies by taking their individual histcs separately. However, when I want to make it a 3D plot, I can't seem to find a way to take the histc of the latitude-longitude pairs.
EDIT: I am adding my code for plotting the bar graphs here upon being asked by one commenter to do so, though I don't see how that would help. The bar graph for latitude is: bar(unique(M), histc(M,unique(M))) and that for longitude is bar(unique(N), histc(N,unique(N))). And the M and N are nx1 matrices. (Actually, they are 2 columns of a much larger matrix. But for simplicity in comprehension, I have avoided writing complex formulae here.)
EDIT: I reckon what I am looking for might be solved by surface plot, surf. But I am not sure. If it is, then the issue I am facing, speaking in terms of surf, can be stated as an issue in defining the Z parameter.
latitudes=180*(rand(1,10000)-0.5);
longitudes=360*(rand(1,10000)-0.5);
d=[latitudes;longitudes];
minVal_Lat=-90;
maxVal_Lat=90;
minVal_Long=-180;
maxVal_Long=180;
delta=10;
axisLat=minVal_Lat:delta:maxVal_Lat;
axisLong=minVal_Long:delta:maxVal_Long;
nPDF_Lat=length(axisLat);
nPDF_Long=length(axisLong);
PDF=zeros(nPDF_Lat,nPDF_Long);
temp=0;
count_i=1;
count_j=1;
for i=axisLat;
lowlimit_x=i-delta/2;
upperlimit_x =i+delta/2;
for j=axisLong;
lowlimit_y=j-delta/2;
upperlimit_y =j+delta/2;
temp=0;
for k=1:length(d(1,:));
if lowlimit_x<=d(1,k) & d(1,k)<upperlimit_x
if lowlimit_y<=d(2,k) & d(2,k)<upperlimit_y
temp=temp+1;
else
end
else
end
end
PDF(count_i,count_j)=temp;
count_j=count_j+1;
end
count_i=count_i+1;
count_j=1;
end
normFactor=sum(sum(PDF));
PDF=(1/normFactor)*PDF;
randVar_Lat=minVal_Lat:delta:maxVal_Lat;
randVar_Long=minVal_Long:delta:maxVal_Long;
surf(randVar_Lat,randVar_Long,PDF')
If you want get a 2D probability density function with surf plot, these codes will be worked.
I'm currently producing a contour plot using
contour(x,y,z)
However, I would like to specify some additional contour lines to the ones provided.
I understand that I can use contour(x,y,z,v) where v is some vector containing values of the contour levels I would like but I don't really want to use this since I don't know exactly the levels.
Instead is it possible to plot the contour that goes through a specific point (x,y)?
Thanks.
You can overplot a second contour with a single, specific value for the contour, optionally specifying parameters like line width to make it obvious:
contour(x,y,z)
hold on
lev = z(n,m); % find the value you want in z
contour(x,y,z,lev,'Linewidth',2);
I am trying to graph a contour graph with a two dimentional matrix, v. v contains velocity data, y-index represents depth, x-index is mapped to a 1d-vector containing latitude info, lat.
when I graph contour(v), the x-axis is index, but I wish to show the latitude (also scale accordingly), I tried contour(lat,v) but it just shows me a blank graph, how should I graph it?
Without having an example dataset, it's hard to say for sure... but I suspect that what you need to do is use the contour(X,Y,Z) form of contour. If you want to specify one axis, you need to specify both. In your case, it would be:
contour(lat,depth,v)
If you're happy using the y-index rather than an actual depth vector, you can do
contour(lat,1:size(v,2),v)
I have a formula that depends on theta and phi (spherical coordinates 0<=theta<=2*pi and 0<=phi<=pi). By inserting each engle, I obtained a quantity. Now I have a set of data for different angles and I need to plot the surface. My data is a 180*360 matrix, so I am not sure if I can use SURF or MESH or PLOT3. The figure should be a surface that include all data and the axes should be in terms of the quantity, not the quantity versus the angles. How can I plot such a surface?
I see no reason why you cannot use mesh or surf to plot such data. Another option I tend to use is that of density plots. You basically display the dependent variable (quantity) as an image and include the independent variables (angles) along the axis, much like you would with the aforementioned 3D plotting functions. This can be done with imagesc.
Typically you would want your axes to be the dependent variables. Could you elaborate more on this point?
If I understand you correctly you have calculated a function f(theta,phi) and now you want to plot the surface containing all the points with the polar coordinated (r,theta,phi) where r=f(theta,phi).
If this is what you want to do, the 2D version of such a plot is included in MATLAB under the name polar. Unfortunately, as you pointed out, polar3 on MatlabCentral is not the generalization you are looking for.
I have been able to plot a sphere with the following code, using constant r=1. You can give it a try with your function:
phi1=0:1/(3*pi):pi; %# this would be your 180 points
theta1=-pi:1/(3*pi):pi; % your 360 points
r=ones(numel(theta1),numel(phi1));
[phi,theta]=meshgrid(phi1,theta1);
x=r.*sin(theta).*cos(phi);
y=r.*sin(theta).*sin(phi);
z=r.*cos(theta);
tri=delaunay(x(:),y(:),z(:));
trisurf(tri,x,y,z);
From my tests it seems that delaunay also includes a lot of triangles which go through the volume of my sphere, so it seems this is not optimal. So maybe you can have a look at fill3 and construct the triangles it draws itself: as a first approximation, you could have the points [x(n,m) x(n+1,m) x(n,m+1)] combined into one triangle, and [x(n+1,m) x(n+1,m+1) x(n+1,m+1)] into another...?