How can I set a color gradient on my bar plot?

Hello!
I have the following vector of values:
[1 2 3 4 5 6 7 8 9 10; -1.8 -1.7 -1.3 -1.4 -1.8 -1.7 -1.6 -1.7 -1.5 -1.4].
The first line corresponds to the x-axis whereas the second is the y-axis. I would like to plot these values in a 2D bar plot with a color gradient. I would like to color the bars according to their height in order to highlight the maximum value (-1.3). How can I do that?
Many thanks in advance!

 Accepted Answer

Robert
Robert on 8 Aug 2016
Edited: Robert on 9 Aug 2016
The most direct way in my opinion is to call bar once for each bar in your plot, assigning individual colors that way.
y = [-1.8 -1.7 -1.3 -1.4 -1.8 -1.7 -1.6 -1.7 -1.5 -1.4];
N = length(y);
clr = interp1(linspace(min(y),max(y),100),parula(100),y);
bar(nan(size(y))); % ghost bar plot to get our axis labels
hold on
for ii = 1:N
bar(ii,y(ii),'FaceColor',clr(ii,:));
hold on
end
hold off
But you may want a way to color existing bar charts and/or that allows you to interact with the barseries object as you would with a single call to bar.
A commenter at undocumentedmatlab.com posted a means of editing a bar plot to have separate colors (in R2014b or newer) by editing the Face property of the bar plot as follows.
y = [-1.8 -1.7 -1.3 -1.4 -1.8 -1.7 -1.6 -1.7 -1.5 -1.4];
N = length(y);
hBar = bar(y); % can use x = 1:N but don't have to
StripData is one way Face organizes its data; VertexIndices is another. We need to switch from StripData to VertexIndices in order to color the bars.
hBar.Face.StripData = []; % Delete the existing StripData
Each bar is described by its four corners. We need to put them in an order that doesn't cross the middle of the rectangle. Here we use 1, 2, 4, 3. You can try vi = 1:4*N here to see why we need to do this.
vi = [1:4:N*4; 2:4:N*4; 4:4:N*4; 3:4:N*4];
vi = uint32(vi(:)');
And then we can map the values to colors with interp1 and colormap (or you can use any other color scheme you want).
clr = interp1(linspace(min(y),max(y),100),parula(100),y);
clr = uint8(clr'*255); % convert to the type and shape we need
clr(4,:) = 255; % we need to add a row for opacity. 255 is fully opaque.
And finally apply our changes.
hBar.Face.VertexIndices = vi;
hBar.Face.ColorBinding = 'discrete';
hBar.Face.ColorData = clr;
Whew! You should see individual colors on each bar now! Each time the plot is updated, you will have to re-apply the changes by executing (again)
hBar.Face.StripData = [];
hBar.Face.VertexIndices = vi;

More Answers (1)

Hello Robert!
Thank you so much for your reply. When I test the code, it does not seem to work. Or maybe there is something I did not understand. Here is what I get:
I would like to have bars with different colours with a color gradient: the bar with the highest value in red, the bar with the lowest value in yellow, and the others in the middle in red-orange, orange-yellow. I don't know if it is feasible though (I am not a Matlab expert at all).
Thanks in advance for your help

3 Comments

What version of MATLAB are you using?
What you see there is similar to what I see after the figure updates and before I run
hBar.Face.StripData = [];
hBar.Face.VertexIndices = vi;
to re-apply the colors.
To get the colors you want, check out the other colormap options. hot and autumn are pretty close to what you described.
I like this method of coloring the bars because it doesn't affect the way you interact with the barseries object (except for the colors of course); however, you might find it easier to try the first approach and call bar once for each bar on your plot and give each their own color that way.
I am using Matlab R2016a.
There is something I still not understand. I ran the code you suggested with a colormap set to autumn and then I ran
% hBar.Face.StripData = [];hBar.Face.VertexIndices = vi;
After that, I got a bar plot with all bars in red. I don't understand what I did wrong.
Thanks for your help
Hi again,
I tried the first option you mentioned in your post and now it works! Thanks a lot

Sign in to comment.

Categories

Asked:

on 8 Aug 2016

Commented:

on 10 Aug 2016

Community Treasure Hunt

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

Start Hunting!