Outline plotted by matlab point clusters

I have two vectors that are paired values

size(X)=1e4 x 1; size(Y)=1e4 x 1 

Is it possible to build any a contour plot , creating contours with the highest density of points? Those. highest clustering = red and then gradient color elsewhere?

If you need more clarification, ask. Respectfully,

DATA EXAMPLE:

 X=[53 58 62 56 72 63 65 57 52 56 52 70 54 54 59 58 71 66 55 56]; Y=[40 33 35 37 33 36 32 36 35 33 41 35 37 31 40 41 34 33 34 37 ]; scatter(X,Y,'ro'); 

enter image description here

Thanks for the help. Also remember that we can use hist3 :

 x={0:0.38/4:0.38}; % # How many bins in x direction y={0:0.65/7:0.65}; % # How many bins in y direction ncount=hist3([XY],'Edges',[xy]); pcolor(ncount./sum(sum(ncount))); colorbar 

Does anyone know why edges in hist3 should be cells?

+6
source share
3 answers

It is mainly a question of evaluating the probability density function that generates your data and then visualizing it in a good and meaningful way, I would say. To this end, I would recommend using a smoother estimate than the histogram, for example, the Parzen window (a generalization of the histogram method).

In my code below, I used your sample dataset and estimated the probability density in a grid adjusted by the range of your data. Here you have 3 variables that you need to configure for use on the source data; Borders, Sigma and stepSize.

 Border = 5; Sigma = 5; stepSize = 1; X=[53 58 62 56 72 63 65 57 52 56 52 70 54 54 59 58 71 66 55 56]; Y=[40 33 35 37 33 36 32 36 35 33 41 35 37 31 40 41 34 33 34 37 ]; D = [X' Y']; N = length(X); Xrange = [min(X)-Border max(X)+Border]; Yrange = [min(Y)-Border max(Y)+Border]; %Setup coordinate grid [XX YY] = meshgrid(Xrange(1):stepSize:Xrange(2), Yrange(1):stepSize:Yrange(2)); YY = flipud(YY); %Parzen parameters and function handle pf1 = @(C1,C2) (1/N)*(1/((2*pi)*Sigma^2)).*... exp(-( (C1(1)-C2(1))^2+ (C1(2)-C2(2))^2)/(2*Sigma^2)); PPDF1 = zeros(size(XX)); %Populate coordinate surface [RC] = size(PPDF1); NN = length(D); for c=1:C for r=1:R for d=1:N PPDF1(r,c) = PPDF1(r,c) + ... pf1([XX(1,c) YY(r,1)],[D(d,1) D(d,2)]); end end end %Normalize data m1 = max(PPDF1(:)); PPDF1 = PPDF1 / m1; %Set up visualization set(0,'defaulttextinterpreter','latex','DefaultAxesFontSize',20) fig = figure(1);clf stem3(D(:,1),D(:,2),zeros(N,1),'b.'); hold on; %Add PDF estimates to figure s1 = surfc(XX,YY,PPDF1);shading interp;alpha(s1,'color'); sub1=gca; view(2) axis([Xrange(1) Xrange(2) Yrange(1) Yrange(2)]) 

enter image description here

Please note: this visualization is actually 3-dimensional:

enter image description here

+9
source

Watch this 4-minute video on mathworks:

http://blogs.mathworks.com/videos/2010/01/22/advanced-making-a-2d-or-3d-histogram-to-visualize-data-density/

I believe that this should be very close to exactly those functions that you need.

+2
source

I would divide the area in which the plot covers the grid, and then counts the number of points in each square of the grid. Here is an example of how this can be done.

 % Get random data with high density X=randn(1e4,1); Y=randn(1e4,1); Xmin=min(X); Xmax=max(X); Ymin=min(Y); Ymax=max(Y); % guess of grid size, could be divided into nx and ny n=floor((length(X))^0.25); % Create x and y-axis x=linspace(Xmin,Xmax,n); y=linspace(Ymin,Ymax,n); dx=x(2)-x(1); dy=y(2)-y(1); griddata=zeros(n); for i=1:length(X) % Calculate which bin the point is positioned in indexX=floor((X(i)-Xmin)/dx)+1; indexY=floor((Y(i)-Ymin)/dy)+1; griddata(indexX,indexY)=griddata(indexX,indexY)+1; end contourf(x,y,griddata) 

Edit: The video in Marm0t's answer uses the same technique, but probably explains it better.

+1
source

Source: https://habr.com/ru/post/907605/


All Articles