Why isn't my color assignment working in this bar graph?

Hi all, I am trying to assign colors to individual bars in a bar plot that I'm making. I was able to do this before using a 3x3 matrix, but now with the 1x3 matrix (called "Slopes" below) I keep getting the error, "??? Index exceeds matrix dimensions. Error in ==> NColdthree at 93 set(bar1(2),'FaceColor','r')" when I try to run the following script:
Slopes = [-0.002 0.010 -0.012];
subplot(1,3,2) bar1 = bar(Slopes);
%Set the colors of the three bars set(bar1(1),'FaceColor','b') set(bar1(2),'FaceColor','r') set(bar1(3),'FaceColor','g')
%Add legend, axis labels, etc. legend(bar1,'P','E','P-E') ylabel 'Regression Coefficients' xlabel('Northern Cold*') title('Linear Trend','FontSize',16) axis([0 4 -0.05 0.05])
Any thoughts on why I can't specify the colors for the 2nd and 3rd bars? Thanks!

Risposte (2)

See attached demo.

4 Commenti

Hi Image Analyst, Thanks for the demo, however it's a little over my head. There are many commands in this script that I am not familiar with, especially in the first part of the demo where the user is defining the number of bars in the bar chart. Is it possible to simplify this?
Let me simplify it by extracting out just the crucial part. You said you want to plot each bar in its own individual color. So I plot each bar one at a time and assign a color to it:
% Plot each number one at a time, calling bar() for each y value.
for b = 1 : numberOfBars
% Plot one single bar as a separate bar series.
handleToThisBarSeries(b) = bar(x(b), y(b), 'BarWidth', 0.9);
% Apply the color to this bar series.
set(handleToThisBarSeries(b),'FaceColor', barColorMap(b,:));
end
The for loop goes over each bar. For example if you have 10 bars, it iterates 10 times to plot the 10 bars - one bar per iteration.
Next it calls the bar() command to plot the bar. However this bar is plotted using the color from the default color order. (I can change that order if you want and have a demo for that if anyone wants it.) But let's say that the default color is blue and you really wanted purple. So we have to change the bar to purple.
That is what the next line does. The call to set() changes the color of the one single bar that we just plotted to the desired color.
So once I explain it line by line, does it make sense now? The rest of the code is just to set up the colors that you want, and to make the plot look fancy, for example by putting the bar value atop the bar, putting up labels, title, captions, etc.
Great, thank you for the clarification.
It sounds like, from your comment to vivek (who's code is the same as mine just with different variable names), that you got it working. If so, please mark my Answer as Accepted, otherwise let's get it figured out.

Accedi per commentare.

Vivek Selvam
Vivek Selvam il 13 Nov 2013
Modificato: Vivek Selvam il 13 Nov 2013
This link says how to. Hope the following illustration helps.
Slopes = [-0.002 0.010 -0.012];
numBars = numel(Slopes);
x = 1:numBars;
colStr = 'rgbymc';
for i = 1:numBars
barHan = bar(x(i),Slopes(i));
set(barHan,'FaceColor',colStr(i));
hold on
end
In case you have a lot of bars (therefore need more colors), you can make use of colormaps ( doc colormap ). For example using jet colormap (replace the corresponding lines in the above code):
colStr = jet(numBars); % line 4
set(barHan,'FaceColor',colStr(i,:)); % line 7

Categorie

Richiesto:

il 13 Nov 2013

Commentato:

il 14 Nov 2013

Community Treasure Hunt

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

Start Hunting!

Translated by