How to plot a density distribution of two variables

3 views (last 30 days)
Nino
Nino on 12 Oct 2013
Answered: nick on 20 Feb 2025
Hello,
i have a huge list of coordinates (x,y) which are often repeated in the list, as you can see from a short cut sample (see attachment):
Since the original huge files creates a surface plot of all these data of (x,y), I would like to plot this surface with the density variation, meaning that I would like to see in the plot where the (x,y) are repeated (high density) and where not.
Can somebody tell me which is the correct function for this?
cheers,
Nino
  1 Comment
sixwwwwww
sixwwwwww on 12 Oct 2013
Dear Nino, attachment is missing. Please try to attach the file once again

Sign in to comment.

Answers (1)

nick
nick on 20 Feb 2025
Hello Nino,
Since the file is not attached, I have used dummy data to reproduce it.
To visualize the density of repeated coordinates in a 2D plot, you can use a 2D histogram with the help of "histogram2" function, available from MATLAB 2015b onwards, as shown below:
% Sample data (replace this with your actual data)
x = [1, 2, 2, 3, 4, 4, 4, 5, 6, 6, 6, 6];
y = [1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7];
numBinsX = 100;
numBinsY = 100;
figure;
histogram2(x, y, [numBinsX, numBinsY], 'DisplayStyle', 'tile', 'ShowEmptyBins', 'on');
colorbar;
xlabel('X');
ylabel('Y');
title('Density Plot of (x, y) Coordinates');
colormap('hot');
grid on;
axis equal tight;
You can refer to the documentation of 'histogram2' to know more about it using the following command in MATLAB command window :
doc histogram2
Hope this helps!

Categories

Find more on Data Distribution 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!