Density distribution of scatter plot?

20 次查看(过去 30 天)
Iron1759
Iron1759 2021-4-20
回答: Nipun 2024-6-6
Hi everyone,
I'd like to get the different data points from each set to a single point that will consist of concentric elipses according to the density of the data points, i.e. bold colour where the density is high and fade colour where the density declines.
I was thinking on contourf function and to determine the 'z value' according to the distance from the data set average, i.e. the z will be higher close to the average. Then, contourf will be applied for each data set with 'hold on'.
Before I try to check whether it can be implemented and looks good, any other metods that might help here?
Cheers!

回答(1 个)

Nipun
Nipun 2024-6-6
Hi Iron,
I understand that you want to visualize the density of data points using concentric ellipses with varying color intensity. Here is a MATLAB approach using the contourf function to achieve this effect:
  1. Calculate the density of data points.
  2. Use contourf to create the density plot.
  3. Overlay different datasets using hold on.
Here's how you can implement this:
% Assuming you have three sets of data: Pristine, Neutron, and Amorphous
% For demonstration, let's create some random data
% Replace these with your actual data
x1 = randn(100, 1) + 8; y1 = randn(100, 1) + 10; % Pristine
x2 = randn(50, 1) + 9; y2 = randn(50, 1) + 9; % Neutron
x3 = randn(30, 1) + 11; y3 = randn(30, 1) + 12; % Amorphous
% Combine data for contour plot
x = [x1; x2; x3];
y = [y1; y2; y3];
% Create a grid for the density plot
xGrid = linspace(min(x), max(x), 100);
yGrid = linspace(min(y), max(y), 100);
[X, Y] = meshgrid(xGrid, yGrid);
% Calculate density using ksdensity
density = ksdensity([x, y], [X(:), Y(:)]);
density = reshape(density, size(X));
% Plot density using contourf
figure;
contourf(X, Y, density, 20, 'LineColor', 'none'); % 20 levels
colorbar;
hold on;
% Overlay original data points
scatter(x1, y1, 'bo'); % Pristine
scatter(x2, y2, 'rx'); % Neutron
scatter(x3, y3, 'g*'); % Amorphous
% Set labels and title
xlabel('Entropy of FFT domain (medium frequency)');
ylabel('Entropy of DCT domain');
title('Scatter plot with density overlay');
% Add legend
legend('Density', 'Pristine', 'Neutron', 'Amorphous');
hold off;
For more information on the functions used:
  • contourf: https://www.mathworks.com/help/matlab/ref/contourf.html
  • ksdensity: https://www.mathworks.com/help/stats/ksdensity.html
  • scatter: https://www.mathworks.com/help/matlab/ref/scatter.html
Hope this helps.
Regards,
Nipun

标签

Community Treasure Hunt

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

Start Hunting!

Translated by