Density MATLAB 2-D

I am trying to make a density plot for data that contains two columns with different ranges. The RMSD column is [0-2] and the Angle is [0-200].

My data in file:

0.0225370 37.088  
0.1049553 35.309  
0.0710002 33.993  
0.0866880 34.708  
0.0912664 33.011  
0.0932054 33.191  
0.1083590 37.276  
0.1104145 34.882  
0.1027977 34.341  
0.0896688 35.991  
0.1047578 36.457  
0.1215936 38.914  
0.1105484 35.051  
0.0974138 35.533  
0.1390955 33.601  
0.1333878 32.133  
0.0933365 35.714  
0.1200465 33.038  
0.1155794 33.694  
0.1125247 34.522  
0.1181806 37.890  
0.1291700 38.871  

      

  • I want both x and y axes to be encoded 1 / 10th of the range
  • 0 as an axis starting at the same
  • Print out the number of elements in each grid of a matrix like this and make a density plot based on that number of elements

       0 0.1 0.2 (RMSD)   
    0  0 1 3
    20 2 0 4
    40 1 0 5
    60 0 0 2
    (Angle)
    
          

I can find ways to do 1-bit binning, but then I am confused about how to make a density plot from these values ​​and didn't even dare to try 2-D binning + plotting.

thanks for the help

+3


source to share


2 answers


I think you want hist3

. Assuming you want to photograph the edges of the bin (not the bin centers), use

result = hist3(data, 'Edges', {[0 .1 .2], [0 20 40 60]}).';

      

where data

stands for your data.

From the linked documentation:



hist3(X,'Edges',edges)

, where edges

is a two-element array of cells of numeric vectors with monotonically non-decreasing values, uses a 2-dimensional mesh of bins with edges in edges{1}

the first dimension and edges{2}

in the second. ( i

, j

) th bin includes the value X(k,:)

if

edges{1}(i) <= X(k,1) < edges{1}(i+1)


edges{2}(j) <= X(k,2) < edges{2}(j+1)

With your example data, this gives

result =
     0     0     0
     8    14     0
     0     0     0
     0     0     0

      

+1


source


For those without the Statistics and Machine Learning Toolbox to run a 2D histogram ( hist3 ), it might be more practical using an alternative to solving the 2D hist problem. The following function generates the same output

function N = hist3_alt(x,y,edgesX,edgesY)
N = zeros(length(edgesY)-1,length(edgesX)-1);
[~,~,binX] = histcounts(x,edgesX);
for ii=1:numel(edgesX)-1
    N(:,ii) = (histcounts(y(binX==ii),edgesY))';
end

      



It's simple and effective. Then you can run this function like this:

N = hist3_alt(x,y,[0:0.1:2],[0:20:200])

      

0


source







All Articles