I could manage to make a colorbar plot using scatter, but I have to show the associated errorbar as well. To bare minimum, I first created errorbar plot and then scatter plot on the same figure with colormap. However, It would be great if the color of the errorbar matches the color of the symbol as per the colorbar values. Note that the colorscale is in the log value. My output figure is following and the data is attached as data_mat.mat file.
My code is following
figure();box on;hold on;
errorbar(data_mat(:,1),data_mat(:,3),data_mat(:,4),'.','MarkerSize',10,'LineWidth',1)
hold on;
scatter(data_mat(:,1),data_mat(:,3),140,data_mat(:,2),'fill');
hcb=colorbar;
colormap('parula');
hcb.Title.String = "title";
set(gca,'ColorScale','log');
set(gca,'FontSize',18);
hcb.FontSize=14;
xlabel('X','FontSize',20);ylabel('Y','FontSize',20);
set(gca, 'Linewidth', 1);

 Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 21 Oct 2022
Edited: Bjorn Gustavsson on 27 Oct 2022
You might get away with putting the errorbar-call in a loop. Perhaps something like this:
figure();
clf
box on
hold on
cmp = parula;
dx = 0.1*randn(size(data_mat(:,1)));
for i1 = size(data_mat,1):-1:1
phE(i1) = errorbar(data_mat(i1,1)+dx(i1),...
data_mat(i1,3),...
data_mat(i1,4),...
'.','MarkerSize',10,'LineWidth',1);
hold on
end
for i1 = size(data_mat,1):-1:1
% Set the colour of phE to a colour from a linear interpolation of the
% full parula colour-map between the max and min values of data_mat(:,4)
set(phE(i1),...
'color',interp1(1:size(cmp,1),...
cmp,...
1+(size(cmp,1)-1)*(log10(data_mat(i1,2))-min(log10(data_mat(:,2))))/(max(log10(data_mat(:,2))) - min(log10(data_mat(:,2))))))
end
hold on;
scatter(data_mat(:,1)+dx,data_mat(:,3),140,data_mat(:,2),'fill');
hcb=colorbar;
colormap('parula');
hcb.Title.String = "title";
set(gca,'ColorScale','log');
set(gca,'FontSize',18);
hcb.FontSize=14;
xlabel('X','FontSize',20);ylabel('Y','FontSize',20);
set(gca, 'Linewidth', 1);
HTH

7 Comments

@Bjorn Gustavsson. Thanks for the answer. Somehow, the section in which "set color" is being done, resulting in only one color for all the data. So, finally it looks similar to the previos one (the output figure with your code is attached). May be something is wrong at my side.
Could you please share the output figure that you got so that I can verify if I am doing anything wrong?
My bad, was sloppy and forgot the first change - to only draw the error-bar for one data-point at a time. Now fixed.
@Bjorn Gustavsson Thanks for the reply. Still, I couldn't get what I wanted to achieve.
Could you please share the output figure of your code?
Thanks
Changed it again to correct the y-coordinates of the error-bar calls.
@Bjorn GustavssonThanks. However, the problem still exists. The color of the errorbar is not according to the symbol (see attached fig in the circled region). For example, in the middle circle, the symbol is orange, but the error bar is yellow in color. Apart from this, in the right or left marked region, there are no yellow errorbar corresponding to the yellow symbol.
I feel the issue is because the color of the symbol is as per the "log" colorscale and error bar color is being done linearly. ( Note - Linear colorscale value of the symbol with the appropriate color of the errorbar is easy to plot just with "errorbar" function using interpolated colormap value as color variable. But I do not want in a linear way.)
Then just change the interpolation-scheme to logarithmic, as corrected above.
@Bjorn Gustavsson Thank you so much! Perfect now. Here is the output figure with little tweaks (-changing the order of "scatter" and "errorbar" function, and making the errorbar symbol big enough to match the scatter symbol).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!