I am plotting a bar
+scatter
plot where the scatter points are colored according to a separate variable. The problem I am having is that the colorbar
is using the wrong values at the moment. If I just plot the scatter
plot and add a colorbar
, then the range of the colorbar
is correct.
I am using Matlab 2016a.
Please find a working example of the code below:
figure
subplot(2,1,1)
a = 1;
b = 2;
r = (b-a).*rand(1,7) + a;
y = r;
rr = (b-a).*rand(1,7) + a;
z = rr;
x = [1:7];
zz = rand(1,7)
yyaxis left
hold on
for i = 1:7
h=bar(i,y(i), 'FaceColor',[1 1 1], 'LineWidth',3);
yb(i) = cat(1, h.YData);
xb(i) = bsxfun(@plus, h(1).XData, [h.XOffset]');
if zz(i) < 0.0300000
set(h,'EdgeColor','k');
elseif zz(i) < 0.050000000
set(h,'EdgeColor','b');
elseif zz(i) < 0.070000000
set(h,'EdgeColor','g');
else
set(h,'EdgeColor','r');
end
end
ylabel('hm', 'FontSize', 12, 'FontWeight', 'bold')
for i1=1:7
t = text(xb(i1)-0.2,yb(i1),num2str(yb(i1),'%0.3f'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
s = t.FontSize;
t.FontSize = 12;
t.FontWeight = 'bold';
end
yyaxis right
pointsize = 40;
hh = scatter(x,z,pointsize, zz,'filled')
cc = colormap([hsv(20)])
c = colorbar
c.Label.String = 'Pos';
set(gca,'Ydir','reverse')
ylabel('OK', 'FontSize', 12, 'FontWeight', 'bold')
lgd = legend([h, hh], 'hm', 'OK')
subplot(2,1,2)
x = [1:8]
a = 1;
b = 2;
r = (b-a).*rand(1,8) + a;
y = r;
rr = (b-a).*rand(1,8) + a;
z = rr;
zz = rand(1,8);
yyaxis left
hold on
for i = 1:8
h=bar(i,y(i), 'FaceColor',[1 1 1], 'LineWidth',3);
yb(i) = cat(1, h.YData);
xb(i) = bsxfun(@plus, h(1).XData, [h.XOffset]');
if zz(i) < 0.0300000
set(h,'EdgeColor','k');
elseif zz(i) < 0.050000000
set(h,'EdgeColor','b');
elseif zz(i) < 0.070000000
set(h,'EdgeColor','g');
else
set(h,'EdgeColor','r');
end
end
for i1=1:8
t = text(xb(i1)-0.2,yb(i1),num2str(yb(i1),'%0.3f'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
s = t.FontSize;
t.FontSize = 12;
t.FontWeight = 'bold';
end
ylabel('hm', 'FontSize', 12, 'FontWeight', 'bold')
yyaxis right
pointsize = 40;
hh = scatter(x,z,pointsize, zz,'filled')
set(gca,'Ydir','reverse')
ylabel('OK', 'FontSize', 12, 'FontWeight', 'bold')
c = colorbar
c.Label.String = 'Pos';
lgd = legend([h, hh], 'hm', 'OK')
%title(lgd,'My Legend Title')
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Edit A working example of one solution to the question.
figure
a = 1;
b = 2;
r = (b-a).*rand(1,7) + a;
y = r;
rr = (b-a).*rand(1,7) + a;
z = rr;
x = [1:7];
zz = rand(1,7)
colormap(jet)
yyaxis left
hold on
for i = 1:length(y)
h=bar(i,y(i), 'FaceColor',[1 1 1], 'LineWidth',3);
yb(i) = cat(1, h.YData);
xb(i) = bsxfun(@plus, h(1).XData, [h.XOffset]');
if zz(i) < 0.0300000
set(h,'EdgeColor','k');
elseif zz(i) < 0.050000000
set(h,'EdgeColor','k');
elseif zz(i) < 0.070000000
set(h,'EdgeColor','k');
else
set(h,'EdgeColor','k');
end
end
cco = min(zz)
cct = max(zz)
caxis([cco cct])
coloo = colorbar
coloo.Label.String = 'Cbar';
%h = bar(y, 0.2, 'FaceColor',[1 1 1], 'EdgeColor',[0 0 0],'LineWidth',2);
%yb = cat(1, h.YData);
%xb = bsxfun(@plus, h(1).XData, [h.XOffset]');
for i1=1:7 % numel(yb)
t = text(xb(i1)-0.3,yb(i1),num2str(yb(i1),'%0.3f'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
s = t.FontSize;
t.FontSize = 12;
t.FontWeight = 'bold';
end
ylabel('OK', 'FontSize', 12, 'FontWeight', 'bold')
yyaxis right
pointsize = 80;
hh = scatter(x,z,pointsize, zz,'filled')
set(gca,'Ydir','reverse')
ylabel('MM', 'FontSize', 12, 'FontWeight', 'bold')
c = colorbar
c.Label.String = 'Cbar';
lgd = legend([h, hh], 'OK', 'MM')
%title(lgd,'My Legend Title')
hold off