MATLAB: I simply want the colors of bars in a bar graph to depend on the y value.

color bar graph

I have a vector of 2026 integers with each entry taking a value between 0 and 11. I make a simple bar chart and I want the color of each bar to vary according to its height (value 0 to 11) for example according to a linear colormap between blue and red. I have tried to do this in the colormap editor by selecting a colormap with an appropriate gradient and changing the "color data min" and "color data max" to 0 and 11 respectively, but the bars always appear to be the same color. I think the problem may be that I'm not using a three dimensional set of data?

Best Answer

NEW ANSWER
Here's a method to do what you want. Modify code to fit your color map scheme.
ClrMap = colormap('jet');
X = 1:10;
Y = X.^2;
Clr = ceil(size(ClrMap, 1) * Y./max(Y));
for k = 1:length(Y)
bar(k, Y(k), 'FaceColor', ClrMap(Clr(k), :));
if k == 1
hold on
end
end
hold off
set(gca, 'XTick', X)
OLD ANSWER
There are some options here: