How to generate a colourmap for a 2D denisty map?

Hi, I am using MATLAB R2020a with a MacOS. I am currently trying to generate a colourmap to represent the density of points on the following plot:
I have used this function by Malcolm McLean to generate a density:
function [ dmap ] = dataDensity(x, y, width, height, limits, fudge)
if(nargin == 4)
limits(1) = min(x);
limits(2) = max(x);
limits(3) = min(y);
limits(4) = max(y);
end
deltax = (limits(2) - limits(1)) / width;
deltay = (limits(4) - limits(3)) / height;
if(nargin < 6)
fudge = sqrt(deltax^2 + deltay^2);
end
dmap = zeros(height, width);
for ii = 0: height - 1
yi = limits(3) + ii * deltay + deltay/2;
for jj = 0 : width - 1
xi = limits(1) + jj * deltax + deltax/2;
dd = 0;
for kk = 1: length(x)
dist2 = (x(kk) - xi)^2 + (y(kk) - yi)^2;
dd = dd + 1 / ( dist2 + fudge);
end
dmap(ii+1,jj+1) = dd;
end
end
end
I have used this function by Malcolm McLean to then generate a plot of this density.
function [ f ] = DataDensityPlot( x, y, levels )
map = dataDensity(x, y, 256, 256);
map = map - min(min(map));
map = floor(map ./ max(max(map)) * (levels-1));
f = figure();
image(map);
colormap(jet(levels));
set(gca, 'XTick', [1 256]);
set(gca, 'XTickLabel', [min(x) max(x)]);
set(gca, 'YTick', [1 256]);
set(gca, 'YTickLabel', [min(y) max(y)]);
uiwait;
end
However, this creates a contourmap as shown.
Instead, I would like to generate a plot where the colour directly corresponds to the density of points rather than having contours, similar to this:
I would very much appreciate suggestions on how to manipulate the function to create the desired colourmap. Thanks in advance

2 Comments

Hi, sorry about that, I deleted the previous question. This one includes more detail and a different approach

Sign in to comment.

 Accepted Answer

It looks like your comparison image has had the color axis manually adjusted (rt click on colorbar and select "Interactive Colormap Shift"), but is either a hist3 or imagesc plot.
I'd be inclined to use hist3.
load v.mat
load w.mat
figure
hist3([v,w],'Nbins',[275 275],"EdgeColor","none",'CDataMode','auto')
view(2)
axis tight
colormap jet
caxis([0 250])
colorbar

4 Comments

It's showing up weird here. Here's what it looks like after playing with the colobar.
For what it's worth, I think the image you shared actually has the w values reversed. Note what I have as positive it has as negative, and vice versa. That may suggest that was created using imagesc, which has (0,0) in the top left instead of bottom left. Then the tick labels were updated, but the underlying values do not follow.
Thank you very much for this - it is exactly what I wanted. May I please ask how you adjusted the colorbar? I have looked into the help documentation but cannot seem to find how you adjusted the gradient effect. Thanks again
Rt click on colorbar and select "Interactive Colormap Shift", then position the cursor on the color bar and click and drag it to modify the color ranges. For example, I clicked on the green area and dragged it down to about 50.
Ah yes, thanks again.

Sign in to comment.

More Answers (0)

Asked:

on 17 Nov 2020

Commented:

on 17 Nov 2020

Community Treasure Hunt

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

Start Hunting!