7
votes

I use hist3() function to plot the density of points. It creates a grid and finds the number of points in each grid, then it creates the plot. But the colors on the plot are discrete. Is there an option to make this distribution smooth, i.e. make transition from one color to another smoother. Now all the cells of the grid have different colors, from grin to yellow and the distribution is not apparent.

I use the following code.

axis equal;
colormap(jet);
n = hist3(final',[40,40]);
n1 = n'; 
n1( size(n,1) + 1 ,size(n,2) + 1 ) = 0; 
xb = linspace(min(final(:,1)),max(final(:,1)),size(n,1)+1);
yb = linspace(min(final(:,2)),max(final(:,2)),size(n,1)+1);
pcolor(xb,yb,n1);

Thanks in advance.

2
check out the colormap(). - Oleg
I use summer colormap. I also tried other colormaps, but it didn't help. I just don't want to see those gird-lines, but want to get a smooth transition from one cell to another. - bordart
Use interp2 to create an interpolated surface from the histogram, then display it. - Bentoy13
I just added the code I use. Could you please thell me how to use that function in this case? - bordart
I discovered Python's matplotlib does smoothing by default of images shown by imshow. If you're willing to try it, it's very much worth it. - rubenvb

2 Answers

8
votes

You may want to use the gridfit function from the matlab file exchange. The smooth effect comes from both the interpolation (more points to plot) and the full use of the available color (colormap jet here). Note that edgecolor is set to noneso that the black lines are removed.

As it is used here, it takes the output of hist3 (20x20 matrix) and interpolate it (100x100). It then plot a surface using surf. In addition, you can uncomment the camlight option.

final = randn(1000,2)';  
n = hist3(final',[20,20]); %binning  
figure('Color','w');  

%your code with pcolor   
subplot(1,2,1);  
axis equal;  
colormap(jet);  
n1 = n';   
n1( size(n,1) + 1 ,size(n,2) + 1 ) = 0;   
xb = linspace(min(final(:,1)),max(final(:,1)),size(n,1)+1);  
yb = linspace(min(final(:,2)),max(final(:,2)),size(n,1)+1);  
pcolor(xb,yb,n1)  


%density with gridfit function  
subplot(1,2,2);  
nb_interp_point = 100;
[x,y] = meshgrid(1:size(n,1),1:size(n,2));  
zgrid = gridfit(x(:), y(:), n, nb_interp_point, nb_interp_point);  
surf(zgrid,'EdgeColor','none')  
set(gca,'YDir','reverse');  
view(-90,90);  
% camlight right  
% lighting phong  

Here is the result

enter image description here

3
votes

To get rid of the gridlines use the following:

hchild=get(gca,'children');
set(hchild,'edgecolor','none')

Colormaps are m x 3 (rgb) arrays. You are free to create your own colormap. For instance you can narrow the range of the colormap you are using, as in the following example:

cmap=colormap(summer);
range = [40:64]; % <-- here I am using a 64 element colorspace 
                 %  and narrowing the selection to the upper range
nc = size(cmap,1);

range = [range(1):(range(end)-range(1))/(nc-1):range(end)];
cmap(:,1)=interp1([1:nc],cmap(:,1),range);
cmap(:,2)=interp1([1:nc],cmap(:,2),range);
cmap(:,3)=interp1([1:nc],cmap(:,3),range);
colormap(cmap)