Scatter 2D plot with specific colorbar

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 日

0 投票

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 年 1 月 31 日
Hello Bjorn,
Thanks for the support. While rumnning your code, I am getting foolowing error msg:
Undefined function 'axP' for input arguments of type 'double'.
Error in Untitled11 (line 4)
axCB = axes('position',[axP(1)+axP(3)+ 0.005 axP(2),0.03 axP(4)]);
Bjorn Gustavsson
Bjorn Gustavsson 2020 年 1 月 31 日
Oops, forgot to copy that line. Corrected the snippet by adding the missing line.
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 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by