Contour line not meeting together - matlab

I have written the following code for a Gaussian copula contour. However, at the bottom left the it is discontinuous. What could be the reason and how can I get a continuous contour?
contour(xgrid,ygrid,Z,[4e-6,4e-6],'EdgeColor',[1 0 0],'LineWidth',2);

Related

Matlab - How to get rid of edge artifact appearing around contour plot

I am facing a problem with the contourf function.
Here is my code, which is part of a gui (hence the handles):
contourLevels = (1:0.5:60);
axes(handles.firstcontouraxes);
contourf(Y,fliplr(X),z1, contourLevels,'edgecolor','none');
axis equal
colormap(cmap);
colorbar();
title('Paramater a plot');
set(gca,'TickDir','out','box','off');
Yielding the following plot:
As you can see, there is a red edge at the interface between the sample and air (air is when the value is 0 and appear white).
I suspect that contourf is trying to smooth my data, as the white area (air) is made of 0s and then we suddenly have relatively larger values at the sample-air interface.
Is it possible to remove this feature?

Distance between broken line and experimental values

I need to evaluate a set of broken lines (red line), returned by a forward model, and select the one best fitting a set of experimental values (green dots); example in the image below. Do you have any suggestion on how to calculate the distance of the points from the broken line?
A decent (I think) approach would be to compute the sum of square distances from the fit to the points.
Assuming that you describe the links between the red dots as straight lines, then you can do it by
Computing the equation of each line between red points.
Compute distances between green points and red points. Assign the line from (1.) between closest 2 red points to each green dot.
Compute distance between each green dot and corresponding (2.) line.
score=sum(sqrt(distances))
If you want interpolate between your red points with something other than linear you may need to find a bit fancier maths, but the process should be the same.

impoly only approximately correct on log-scale axes

When determining points within polygons using MATLAB's inpolygon function, I find that the results are exactly correct for polygons drawn on linear axes but only approximately correct for polygons drawn on log-scale axes. Although my suspicions lean in favor of a MATLAB bug, it's possible I've overlooked something.
The following code reproduces the issue I have been experiencing with other data. The results are shown in the following image (the bottom set of panels are zoomed views of the top panels). One can appreciate that there are unlabeled points inside the polygon and labeled points outside the polygon, neither of which should occur, in the case of a polygon drawn on log-scale axes (right). In contrast, the polygon test is exact for polygons drawn on linear axes (left).
n=2E4;
x(:,1)=rand(n,1); y(:,1)=rand(n,1);
x(:,2)=lognrnd(0.5,0.25,n,1); y(:,2)=lognrnd(0.5,0.25,n,1);
for m=1:2
subplot(1,2,m);
scatter(x(:,m),y(:,m),'.'); hold on;
if(m==2)
set(gca,'xscale','log'); set(gca,'yscale','log');
end
p=impoly(gca);
pc=getPosition(p);
in=inpolygon(x(:,m),y(:,m),pc(:,1),pc(:,2));
scatter(x(in,m),y(in,m),20);
end
I think you missed something: A line in normal scale is not a line in log scale. Your polygons are not properly drawn in the log scale, as you draw 2 points and put them together with a straight line.
Look at the real polygon in log space:
close all
clear
n=2e4;
x(:,1)=rand(n,1); y(:,1)=rand(n,1);
x(:,2)=lognrnd(0.5,0.25,n,1); y(:,2)=lognrnd(0.5,0.25,n,1);
for m=1:2
subplot(1,2,m);
scatter(x(:,m),y(:,m),'.'); hold on;
if(m==2)
set(gca,'xscale','log'); set(gca,'yscale','log');
end
p=impoly(gca);
pc=getPosition(p);
% plot polygon
hold on
for ii=1:size(pc,1)-1
plot(linspace(pc(ii,1),pc(ii+1,1),100),linspace(pc(ii,2),pc(ii+1,2),100),'g')
end
plot(linspace(pc(end,1),pc(1,1),100),linspace(pc(end,2),pc(1,2),100),'g')
in=inpolygon(x(:,m),y(:,m),pc(:,1),pc(:,2));
scatter(x(in,m),y(in,m),20);
end
Look at this zoomed in result (click to enlarge):
This happens because the polygon is defined in euclidean space, and it is defined as points linked by lines. If you want to work in log space, things may get complicated. One way to numerically approximate it is the inverse of what I did for plotting. Create dense enough sampled straight line on log space, convert it to linear space, and define a high vertex polygon with the resulting points. Then use inpolygon.

area plot function

I've been trying to use the MATLAB 'area' plotting function (filled line plot) however when I plot with a log y-scale, the plot is just a line with no "fill". Am I missing something here?
My guess is that the y-coordinates of your area are very big. If you have a look to the log function, the values above 1 are heavily shrunk.
Thus, the y-side of your area goes from a segment to a point. If you try to zoom a lot in the y-axis, you may see the area as a rectangle instead of a line.

Matlab: showing anomalies in a plot

How can make I tiny sticks like those shown in red surroundings in the plot below? I used a binary vector of anomalies (1 if anomaly 0 if not) and used plot function, but rather I' d like to see spikes like in the figure.
These sticks correspond to anomaly indicators in the data such as discontinuities (as can be seen in the plot). I'd also like if you could propose good alternative visualizations.
Matlab allows drawing rectangles on the plot.