How to plot a density distribution of two variables

조회 수: 13 (최근 30일)
Nino
Nino 2013년 10월 12일
답변: nick 2025년 2월 20일
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
sixwwwwww
sixwwwwww 2013년 10월 12일
Dear Nino, attachment is missing. Please try to attach the file once again

댓글을 달려면 로그인하십시오.

답변 (1개)

nick
nick 2025년 2월 20일
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!

Community Treasure Hunt

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

Start Hunting!

Translated by