I would like to plot a bar graph and colour each single bar based on the intensity of the data value

5 views (last 30 days)
When plotting a bar I would like for example that the most high bar will be black, the lower one white and the middle ones grayscale. I need this also for a 3-D bar graph (bar3). thanks all!

Answers (1)

the cyclist
the cyclist on 25 Aug 2011
Setting the color of bar graphs is a bit tricky. Here's one way. You could set the values in the colormap to vary according to your intensity.
% the data
v=ceil(5*ones(1,3));
bh=bar(v);
% - change the bars' colors
ch=get(bh,'children');
cdm=repmat(1:numel(v),5,1);
cdm=[cdm(:);nan];
set(ch,'facevertexcdata',cdm);
% - play with colormaps [if size(colormap,1)=number of bars,
% then each bar will the color of that row of the colormap]
colormap([[1 0 0]; ...
[0 1 0]; ...
[0 0 1]]);

Community Treasure Hunt

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

Start Hunting!