Choosing points from Matlab contour function - matlab

I was wondering how I can view point values off a contour plot.
I wish to obtain all the x-positions at the initial time, t=0 from my contour plot. Can this be done?
I have read on this forum about Choosing isolines from Matlab contour function however I wish to get the actual points.
Any suggestions is much appreciated.

Untested, but recall it to be something like this:
h = findobj(gcf,'Type','Line');
x = get(h,'XData');
y = get(h,'YData');

Related

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 Polarplot() and surface color

I am a bit struggling with my polar plot. I am playing with strikes and dips, and for each pair of those, an "intensity". I'd like to plot this surface/contourf/whatever function on my polarplot. I cannot find the handle to do so. Dpp2 contains the intensity value for a given theta and rho/ strike and dip.
xTmp = (0:4:360);
yTmp = (0:22.5:90);
[strike,dip]= meshgrid(deg2rad(xTmp),deg2rad(yTmp));
dip2 = rad2deg(dip);
strike2 =rad2deg(strike);
figure('name', 'COLD');
polarplot([0 360],[0 90]);
s = surf(strike2, dip2, DPp2);
polarplot(s);
colormap
I've tried something like that, which obviously doesn't work.
cheers,
Flo
As far as I know there is no way of creating a surface plot directly in a polarplot.
One workaround is to manually create your polar axis plot. You can find an example here.
Another workaround would be to use
polarscatter to create a scatter plot (which looks simmilar in case you have a tight grid) Have a look at this.
Because you mentioned the handle: In case you want a handle to the axes have a look at polaraxes from here.
The polar scatter wasn't working, so I tried another function, which seems to work according to this page: https://fr.mathworks.com/matlabcentral/answers/95796-how-do-i-create-a-contour-plot-in-polar-coordinates
I am note quite there yet, the contour map isn't "wrapped" around my polar plot, but so far it's compiling. If anyone has an idea on how to superimpose the contour map onto the polar plot ?
dip2 = rad2deg(dip);
strike2 =rad2deg(strike);
h = polar([0 360],[0 90]);
hold on;
contourf(strike2,dip2,DPp2);
% Hide the POLAR function data and leave annotations
set(h,'Visible','off')
% Turn off axes and set square aspect ratio
axis off
axis image

Overlay the data points which make-up a contour plot matrix on the same plot in 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.

matlab contour plot time depth density

I need assistance making a nice contour plot. I have data from an underwater glider that dives and climbs repeatedly from the surface of the ocean to around 30 m, in this case.
I think my issue is with interpolating the data, and I am not sure how to proceed. Here is the contour plot of density I have generated this far.
The Contour plot of density was generated using this code
xlin = linspace(min(time),max(time),500);
ylin = linspace(min(depth),max(depth),500);
[X,Y] = meshgrid(xlin,ylin);
Z = griddata(time,depth,density,X,Y);
[C,h] = contour(X,Y,Z,[1022.0, 1022.5, 1023.0, 1023.5, 1024.0, 1024.5, 1025.0, 1025.5, 1026.0],'color',[0.5 0.5 0.5]);
v = [1022.0, 1022.5, 1023.0, 1023.5, 1024.0, 1024.5, 1025.0, 1025.5, 1026.0];
clabel(C,h,v,'fontsize',8);
set(gca,'ydir','reverse');
I want the plot to have smooth contour lines. Once I get the contour plot to look good I will overlay it on salinity and temperature scatter plots.
Please let me know how I can make a better looking contour plot.
Is it an issue with interpolation? Or the way I gridded the data?
Thanks very much! Please be specific and give code examples if you've played with the data.
Here is the time, depth, and density matlab data: https://www.dropbox.com/s/agi70zh7haggf07/data.mat?dl=0
The problem is that bunch of your interpolated data are missing. I mean that Z has a bunch of NaNs:
xlin = linspace(min(time),max(time),500);
ylin = linspace(min(depth),max(depth),500);
[X,Y] = meshgrid(xlin,ylin);
Z = griddata(time,depth,density,X,Y);
%surf(X,Y,Z) %also interesting
spy(isnan(Z));
Result:
Your input data are somehow ill-defined, and griddata gives up. Here's why:
>> sum(isnan(density))
ans =
3174
Fix the NaNs in your raw data, and you'll most probably fix the plot.
Update
I threw away your NaNs:
inds=~isnan(density);
time=time(inds);
depth=depth(inds);
density=density(inds);
to see how the result looks like. It turns out that your original code is already looking OK to me!
Original on the left, de-NaNed version on the right:
So... maybe your datetime transformation is off? Or your time limits, not showed in your original code?

MATLAB - fill ezpolar plot

I just have a brief question regarding MatLab.
Say that we have the equation:
r^2 = 2 sin(5t)
I know that I can fill a polar plot by writing, say:
t = linspace(0,2*pi,200);
r = sqrt(abs(2*sin(5*t)));
x = r.*cos(t);
y = r.*sin(t);
fill(x,y,'k')
But say I use the ezpolar instead by giving the equation above a function handle and then typing:
ezpolar(function handle)
Is there any way I can then fill this polar plot? Or do I have to use the procedure outlined above?
Any tips/help will be greatly appreciated!
You can use ezpolar, then modify the resulting figure. If you look at the returned handle from ezpolar, you'll see it is the line itself drawn in the axis. The points from that line object can be extracted, then used to lay a new polygon on top of the same axis. The benefit is, you get to keep all the nice polar lables.
h=ezpolar('sqrt(abs(2*sin(5*t)))')
hold on;
fill(get(h, 'XData'), get(h, 'YData'), 'k');