How to perform a scatter plot based on density in MATLAB?
11 views (last 30 days)
Show older comments
MathWorks Support Team
on 31 Oct 2017
Answered: MathWorks Support Team
on 3 Jan 2018
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.
Accepted Answer
MathWorks Support Team
on 31 Oct 2017
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 Comments
More Answers (0)
See Also
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!