MATLAB Answers

using Matlab to plot density contour for scatter plot

조회 수: 456(최근 30일)
Keqiao Li
Keqiao Li 30 Jun 2015
편집: Cris LaPierre 29 Oct 2020
Hi guys, I'm trying to use Matlab to plot the density contour for the following scatter plot. The bins will be 1X1 box. Within my limited understanding the density contour should be indicated how many points fall into the bins(correct me if I'm wrong), but I still cannot find a way to do the density plot. Need some help to solve this problem. Thanks!

  댓글 수: 0

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

채택된 답변

Mike Garrity
Mike Garrity 30 Jun 2015
Scatter doesn't do any binning. It's showing each individual data element.
Perhaps this approach does what you want?
x = randn(1,1000);
y = randn(1,1000);
n = hist3([x', y']);
pcolor(n)

  댓글 수: 4

표시 이전 댓글 수: 1
Mike Garrity
Mike Garrity 30 Jun 2015
That's fine, you can use contour on the output of hist3.
There are a couple of issues though. First, hist3 isn't in core MATLAB, it's in the Statistics toolbox. The second is that to get the picture you've got above, you're going to need the locations of the bins that hist3 used. The easiest way to do that is to use the two output arg form:
[n,c] = hist3(...)
That second argument 'c' is a cell array which contains the X & Y coordinates of the centers of the bins. You can pass those into contour. The result would look something like this:
x = randn(1,5000);
y = randn(1,5000);
scatter(x,y,'r.')
hold on
[n,c] = hist3([x', y']);
contour(c{1},c{2},n)
I'll let you add your fit lines.
Keqiao Li
Keqiao Li 2 Jul 2015
Thank you Mike, I got what I want. Thank you for your help.
Andrea Ackroyd
Andrea Ackroyd 3 Dec 2018
I am commenting on this old post because I am doing a very similar thing, but achieving some unexpected results.
I am making a histogram2 (tile view) of one dataset, which is time samples of two variables. On the same axes I plot a contour of a map which comes from different data (essentially a target map for the time based data which is in the histogram).
The unusual behaviour is that the colour scale of the hist2 appears to be affected by having the contour on the same axes.
If it is noteworthy, I am plotting the contour first, with a solid colour for all contour lines (not using colormap). Then I plot the histogram second.

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

추가 답변(3개)

Walter Roberson
Walter Roberson 30 Jun 2015
Assuming that your data starts at 0 and contains fractional values and is to be binned by 1 x 1, and that your coordinates are in X and Y,
xidx = 1 + floor(X(:));
maxxoff = max(xidx) - 1;
yidx = 1 + floor(Y(:));
maxyoff = max(yidx) - 1;
counts = accumarray([yidx, xidx], 1); %remember Y corresponds to rows
imagesc(counts, 'XData', 1/2+[0 maxxoff], 'YData', 1/2+[0 maxyoff]);
hold on
contour(1/2+(0:maxxoff), 1/2+(0:maxyoff), counts)
The 1/2 offsets have to do with referencing the centers of the boxes.

  댓글 수: 0

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


Elena De Angelis
Elena De Angelis 14 Jan 2016
편집: Walter Roberson 14 Jan 2016
Hi, I'm trying to do the same but my result is not what I expect, could someone tell me please what i'm doing wrong?
x = dNBR_filt_CHANGE;
y = NBR_post_CHANGE;
scatter(x,y,'r.')
hold on
[n,c] = hist3([x, y]);
contour(c{1},c{2},n)

  댓글 수: 3

Walter Roberson
Walter Roberson 14 Jan 2016
Just in case they are row vectors,
[n,c] = hist3([x(:), y(:)]);
geopap
geopap 12 May 2016
I have exactly the same issue and unfortunately this doesn't work. Any other possible solution?
Update: In my case, where x and y are 1-column vectors, the n variable should be n'
Hamdullah OZOGUL
Hamdullah OZOGUL 1 Feb 2018
You just have to transpose the n to have the correct order.
contour(c{1},c{2},n')

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


Jos? Manuel Amigo
Jos? Manuel Amigo 11 Nov 2017
PCx = rand(2000,1); PCy = rand(2000,1);
bins = 50; Msize = 10;
[N C] = hist3([PCx,PCy],[bins bins]);
CX = C{1}; CY = C{2};
N2 = N; N2(N2 == 0) = []; Nunique = unique(N2);
colors = jet(length(Nunique));
for i = 1:length(PCx) if isnan(PCx(i)) PCxnew(i,1) = NaN; PCynew(i,1) = NaN; J(i,1) = NaN; else whichoneX = find(min(abs(CX - PCx(i))) == abs(CX - PCx(i))); PCxnew(i,1) = CX(whichoneX(1)); whichoneY = find(min(abs(CY - PCy(i))) == abs(CY - PCy(i))); PCynew(i,1) = CY(whichoneY(1)); J(i,1) = sub2ind([bins,bins],whichoneX(1),whichoneY(1)); end end
for i = 1:bins for j = 1:bins temp = sub2ind([bins,bins],i,j); Jthese = find(J == temp); if ~isempty(Jthese) Ntemp = N(temp); Nthis = find(Nunique == Ntemp); plot(PCx(Jthese),PCy(Jthese),'.','color',colors(Nthis,:),'Markersize',Msize); hold on; end end end hold off;

  댓글 수: 1

Jos? Manuel Amigo
Jos? Manuel Amigo 11 Nov 2017
Sorry for the bad pasting. But if you copy-paste, it works

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

Community Treasure Hunt

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

Start Hunting!

Translated by