Improving scatter plot in Matlab - matlab

I have to do the scatter plot of a 2-dimensional region in Matlab.
The collection of the points (x,y) that should be included in the scatter is obtained by running a computationally intense code. As a result, this is the scatter that I get
I don't like the picture because in principle there should be no white dots (i.e., spaces among the scatter dots) inside the blue region. The white dots are there because, given that the points to be included in the scatter are obtained by running a computationally intense code, as a result I get a very coarse grid of points to plot.
I tried to cheat by increasing the size of the scatter dots but the result is even worse as the region looks more and more waving on the borders.
Is there anything I could do to "manually" fill the white spaces inside the blue area? Other ideas?

If you want the whole region to be filled, a patch object might be better suited to your needs. Not knowing how you're generating the points, that might be easier said than done. If you are systematically searching the whole area or something like that, it shouldn't be too hard to identify the edges, or define small patches for each square space on the plot.

Related

Making high quality overlapping inequality plots in Matlab?

I need to make publication quality plots showing domains in parameter space bounded by various inequalities (f1(x,y)>0, f2(x,y)>0, ...) where some regions will satisfy several inequalities and should have blended flat colours.
There are various proposed methods of making inequality plots, but none of them seem to produce a great result:
Using a meshgrid with imagesc will introduce jagged edges (unless I
use a ridiculously large matrix to reach print resolution).
Contourf(x,y,f1,[0 0]) and hold on almost gets what I want, except that it is apparently impossible to give the contoured regions transparency.
Using surf(x,y,f1),
a white z=0 plane for clipping, and view(0,90) also looks good and
enables transparency, but were I to plot two or more inequalities
artefacts are introduced since the colour is different if
f1(x,y)>f2(x,y) or f2(x,y)>f1(x,y).
Stacking axes on top of each other with the
previous method fails since the clipping plane is not transparent.
Taking the countour matrix and filling using fill(C(1,:),C(2,:)) runs into trouble when the
countour reaches the edge of the axes on two sides, since now 1,2, or even
3 corner points are lacking and the fill curve will be closed by a straight line crossing the image.
Anybody know a way of achieving this? Either by making transparent contourf regions, surfaces whose colours combine in the same way regardless of ordering, a way of z-clipping surfaces using a transparent clipping plane, or an algorithm that adds the necessary extra edge points to the fill contour.

How to draw a best fit mesh on a set of points in 2D

I have a problem where we have a grid of points and I'd like to fit a "deformed grid which would best fit the set of points.
The MatLab data can be found at:
https://drive.google.com/file/d/14fKKEC5BKGDOjzWupWFSmythqUrRXae4/view?usp=sharing
You will see that cenX and cenY are the x and y coordinates of these centroids.
Like on this image. To note is that there are points missing, and there are a few extra points. Moreover, You can see some lines are not one single line from left to right, however, we could safely assume that the fitting a line somewhat horizontally (+-5degrees) would properly link the points into a somewhat deformed grid.
The vertical lines are trivial because that is how we generated these dots. We can find the number of lines required through a mode of the count of points on each of the columns of the grid.
I'd like to be able to ensure that a point is only part of one line, as this is a grid.

MATLAB - Filling a map with a gradient of two colors based on city colors

I have a map where each city is classified somewhere between r=0 and r=1, which I have plotted in the following image using the color [r 0 1-r], which gives me a color between red and blue for each city. In this case, the circles are just for testing, and should not be visible in the final image. Currently the circle radius represents the amount of data available for each city. The country outline uses the borders data. Here's the result so far:
My goal is to fill the map with a non-linear gradient of red/blue, taking into account this data and extrapolating it.
(In this example the border seems pretty linear, but that's not always the case, hence the need for a non-linear gradient)
Can anyone point me in the right direction? Thank you!
What you're probably looking for is griddata, which lets you interpolate (unevenly) scattered data points using various interpolation methods. So you could evaluate the interpolation on a grid (which can be created using meshgrid or ndgrid) and then e.g. use surf to plot the interpolated function.

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.

Finding largest inscribed square in a plot

I'm trying to find the largest inscribed square between two lines that intersect at two separate points on a MATLAB plot. First I import two different sets of data as arrays.
Then, after holding the figure, I call the commands
plot(dataSet1,dataSet2)
plot(dataSet2,dataSet1)
and have both of those plots on the same figure. This gives a graph that looks something like this:
My goal is to find the largest square that will fit inside those two lines. Most of what I have seen online is related to black and white matrices, which I believe isn't quite the same as this.