How to perform a scatter plot based on density in MATLAB?

조회 수: 10 (최근 30일)
How to perform a scatter plot based on density in MATLAB?
I have two vectors x & y and I am looking for a way to create a scatter plot of x & y based on their degree of closeness or density in MATLAB.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2017년 10월 31일
The 'scatplot' command takes in column matrix x & y and performs a density based scatter plot as shown in this example:
>> x = randn(1,1000);
>> y = randn(1,1000);
>> scatplot(x,y);
>> colorbar;
The link to the 'scatplot' can be found here :
https://www.mathworks.com/matlabcentral/fileexchange/8577-scatplot
Another File Exchange function called 'dscatter' performs a similar plot.
The File Exchange link can be found here:
>> x = randn(1000,1);
>> y = randn(1000,1);
>> dscatter(x,y)
We can visualize the density within a grid in MATLAB by binning using the hist3 function and subsequently plotting it as follows: 
>> x = randn(1,1000);
>> y = randn(1,1000);
>> n = hist3([x', y']);
>> pcolor(n);
To query the coordinates for a color range, please refer the following:
The function 'getDataForColorRange' in the attachment accepts an Axes handle returned by the 'dscatter' function and a color range.
For example, let us plot the following:
>> x = randn(1000,1);
>> y = randn(1000,1);
>> f = dscatter(x,y);
>> colorbar;
We can query all the X and Y coordinates that are present in the color range 0.7 to 0.9 as follows:
>> [x,y,idx] = getDataForColorRange(f,[0.7 0.9]);
To view this data, we can update the scatter plot as follows:
>> c = f.Children;
>> c.CData(~idx) = NaN;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by