Scatter 2D plot with specific colorbar

5 次查看(过去 30 天)
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.
  1 个评论
Adam
Adam 2020-1-31
You can create a colourmap either manually in code (an nx3 matrix), or using
doc colourmapeditor
for a visual colourmap creation. Then
doc colormap
will apply it to an axes.

请先登录,再进行评论。

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2020-1-31
编辑:Bjorn Gustavsson 2020-1-31
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
  4 个评论
RN
RN 2020-2-1
Thanks Bjorn,
It worked this time. Can you help me to set the colours in colourbar as shown in attached image.
From your code I am getting a colour bar which starts with blue and ends with red. But I need the colorbars as shown in the attached fig.
I am trying to get a colorbar which starts with light red>red>orange>yellow>green >blue, in a gradient fashion.
Thanks again.
Bjorn Gustavsson
Bjorn Gustavsson 2020-2-1
Ramesh, look at the comment from Adam, if you follow his recommendation you will find tools to manually draw a colourmap that fits your needs. You should also make a habit out of searching for solutions on the file exchange, there you will find large number of great solutions to problems - and even if you only find toold that solves 80-90 % of a problem that will only leave you with 10 - 20 % of the work.
One thing to learn is that colormaps in matlab is nothing but n-by-3 double arrays with all elements between 0 and 1. After that you can compose colourmaps in any desired way. If you want to extract the colourmap in the figure you can do something like:
Im1 = imread('fig. 2.jpg');
Cmap_graypinkredorangegreencyanblue = squeeze(mean(Im1(338:-1:20,14:22,:),2));
Cmap_graypinkredorangegreencyanblue = Cmap_graypinkredorangegreencyanblue/max(Cmap_graypinkredorangegreencyanblue(:));
imagesc(peaks)
colorbar
colormap(Cmap_graypinkredorangegreencyanblue)
HTH

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Blue 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by