Barchart colorbar colors from second vector
I have a variant of this question:
https://au.mathworks.com/matlabcentral/answers/506415-bar-chart-legend-and-colour?s_tid=sug_su
I have a bar chart where I am colouring the bars based on a second corresponding vector.
%%
clear
close all
clc
ids1 = [2,4,5,6,8];
meanVals = [0.2,0.204,0.199,0.208,0.19];
velMns = [16.384,16.98,17.182,18.001,18.40];
figure;
b=bar(ids1,meanVals);
xticks(ids1)
grid on
labels = pad(string(b(1).YData),6);
labelsShrt=[extractBetween(labels,1,5)]’;
xtips = b(1).XEndPoints;
ytips = b(1).YEndPoints;
text(xtips,ytips,labelsShrt,’HorizontalAlignment’,’center’,…
‘VerticalAlignment’,’bottom’,’FontSize’,8);
title(‘mean values’)
ylim([min(meanVals)-0.02 max(meanVals)+0.005])
% set bar color
MaxV = 20;
MinV = 5;
range=MaxV-MinV;
colors = jet(range); % Define a colormap
b.FaceColor = ‘flat’;
for II = 1:length(ids1)
barColorID = round(velMns(II),0)-MinV;
b.CData(II,:) = colors(barColorID,:);
end
cbar = colorbar;
caxis([MinV MaxV]);
This works as desired apart from the colormap of the colorbar, which does not correspond to the second vector.
Thanks in advanceI have a variant of this question:
https://au.mathworks.com/matlabcentral/answers/506415-bar-chart-legend-and-colour?s_tid=sug_su
I have a bar chart where I am colouring the bars based on a second corresponding vector.
%%
clear
close all
clc
ids1 = [2,4,5,6,8];
meanVals = [0.2,0.204,0.199,0.208,0.19];
velMns = [16.384,16.98,17.182,18.001,18.40];
figure;
b=bar(ids1,meanVals);
xticks(ids1)
grid on
labels = pad(string(b(1).YData),6);
labelsShrt=[extractBetween(labels,1,5)]’;
xtips = b(1).XEndPoints;
ytips = b(1).YEndPoints;
text(xtips,ytips,labelsShrt,’HorizontalAlignment’,’center’,…
‘VerticalAlignment’,’bottom’,’FontSize’,8);
title(‘mean values’)
ylim([min(meanVals)-0.02 max(meanVals)+0.005])
% set bar color
MaxV = 20;
MinV = 5;
range=MaxV-MinV;
colors = jet(range); % Define a colormap
b.FaceColor = ‘flat’;
for II = 1:length(ids1)
barColorID = round(velMns(II),0)-MinV;
b.CData(II,:) = colors(barColorID,:);
end
cbar = colorbar;
caxis([MinV MaxV]);
This works as desired apart from the colormap of the colorbar, which does not correspond to the second vector.
Thanks in advance I have a variant of this question:
https://au.mathworks.com/matlabcentral/answers/506415-bar-chart-legend-and-colour?s_tid=sug_su
I have a bar chart where I am colouring the bars based on a second corresponding vector.
%%
clear
close all
clc
ids1 = [2,4,5,6,8];
meanVals = [0.2,0.204,0.199,0.208,0.19];
velMns = [16.384,16.98,17.182,18.001,18.40];
figure;
b=bar(ids1,meanVals);
xticks(ids1)
grid on
labels = pad(string(b(1).YData),6);
labelsShrt=[extractBetween(labels,1,5)]’;
xtips = b(1).XEndPoints;
ytips = b(1).YEndPoints;
text(xtips,ytips,labelsShrt,’HorizontalAlignment’,’center’,…
‘VerticalAlignment’,’bottom’,’FontSize’,8);
title(‘mean values’)
ylim([min(meanVals)-0.02 max(meanVals)+0.005])
% set bar color
MaxV = 20;
MinV = 5;
range=MaxV-MinV;
colors = jet(range); % Define a colormap
b.FaceColor = ‘flat’;
for II = 1:length(ids1)
barColorID = round(velMns(II),0)-MinV;
b.CData(II,:) = colors(barColorID,:);
end
cbar = colorbar;
caxis([MinV MaxV]);
This works as desired apart from the colormap of the colorbar, which does not correspond to the second vector.
Thanks in advance bar, colorbar MATLAB Answers — New Questions