MATLAB: Scatter 2D plot with specific colorbar

colorbarscatter

I have three coloumn vectors x, y and z. I have made a scatter plot of x and y with z representing the number of occurrence of 'y' in different colours. See the fig. 1 attached.
Now I need a specific colorbar as shown in fig. 2.
Can anyone help me with the codes in R2014a Matlab.

Best Answer

You can try something like this:
imagesc % Just to get something to show
axP = get(gca,'position');
% Create axes for colorbar
axCB = axes('position',[axP(1)+axP(3)+ 0.005 axP(2),0.03 axP(4)]);
X = logspace(0,log10(2^11),101)';
pcolor([1 2],X,log10([X,X]))
set(gca,'yscale','log')
shading flat
set(gca,...
'yaxisLocation','right','ytick',[1:9,10:10:90,100:100:1000 2048],...
'yticklabel',{'1','','','','','','','','',...
'10','','','','','','','','',...
'100','','','','','','','','',...
'1000','2048'},...
'tickdir','out',...
'box','off',...
'xtick',[])
You might have to doodle a bit with the position of the main axes and the colorbar-axes to get the best layout for your case, and select a suitable colormap.
HTH