MATLAB Answers

second color of bar() always black?

8 views (last 30 days)
David Doria
David Doria on 21 Mar 2012
I am trying to plot two bar plots on the same graph.
When I do:
bar(aout1, a1, 'r'); hold on;
I see the red bars, as expected. However, then when I do:
bar(bout1, b1, 'g');
I see black bars drawn over the red bars.
If I change both "bar" commands to "plot", the colors are red and green, as expected.
Anyone know what could cause this?

Answers (2)

Image Analyst
Image Analyst on 21 Mar 2012
Here is a demo to set customized series colors for both line plots using plot() and bar charts using bar().
% In the help it says this:
% "plot automatically chooses colors and line styles in the order
% specified by ColorOrder and LineStyleOrder properties of current axes.
% ColorOrder : m-by-3 matrix of RGB values
% Colors to use for multiline plots. Defines the colors used
% by the plot and plot3 functions to color each line plotted.
% If you do not specify a line color with plot and plot3,
% these functions cycle through the ColorOrder property to
% obtain the color for each line plotted. To obtain the current ColorOrder,
% which might be set during startup, get the property value:
% get(gca,'ColorOrder')
% Note that if the axes NextPlot property is replace (the default),
% high-level functions like plot reset the ColorOrder property
% before determining the colors to use. If you want MATLAB to
% use a ColorOrder that is different from the default,
% set NextPlot to replacechildren. You can also specify your
% own default ColorOrder."
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
originalColorOrder = get(gca,'ColorOrder') % Initial
% Change to new colors. You can get these via Photoshop if you want.
% Make sure you divide by 255 to get them in the range 0-1.
myColorOrder = [...
255 0 0;... % Red.
0 255 0;... % Green
0 0 255;... % Blue
0 255 255;... % Cyan
255 0 255;... % Magenta
255 255 0;... % Yellow
0 0 0;... % Black
255 128 0]/255 % Orange
% You can extend this by more rows if you want more colors.
numberOfColors = size(myColorOrder, 1)
set(gca, 'ColorOrder', myColorOrder, 'NextPlot', 'replacechildren');
% The things I put in the set() command are especially important.
newColorOrder = get(gca,'ColorOrder') % Verify it changed
% newColorOrder should be the same as myColorOrder
promptMessage = sprintf('Do you want a bar chart, line plot,\nor Cancel to quit?');
button = questdlg(promptMessage, 'Which type of chart', 'Line Plot', 'Bar Chart', 'Cancel');
if strcmp(button, 'Cancel')
elseif strcmpi(button, 'Line Plot');
linePlot = true;
numberOfXSamples = 30; % Good values are 30 for plot and numberOfColors for bar.
linePlot = false;
numberOfXSamples = numberOfColors; % Good values are 30 for plot and numberOfColors for bar.
% Make up some random sample data:
x = 1:numberOfXSamples;
y = zeros(numberOfColors, numberOfXSamples);
for k = 1 : numberOfColors % 8 sets of data.
y(k,:) = k + rand(1, numberOfXSamples);
if linePlot
% Lineplot
% Now plot with changed colors.
plot(x,y, 'LineWidth', 3);
% Bar chart:
bar(x, y, 'BarWidth', 1);
grid on;
fontSize = 20;
title('Plot of 8 lines with custom ColorOrder', 'FontSize', fontSize);
xlabel('X Axis', 'FontSize', fontSize);
ylabel('Y Axis', 'FontSize', fontSize);
legend('Plot 1 = Red', 'Plot 2 = Green', 'Plot 3 = Blue', ...
'Plot 4 = Cyan', 'Plot 5 = Magenta', ...
'Plot 6 = Yellow', 'Plot 7 = Black', 'Plot 8 = Orange');
% Scale the y axis from 0 to 9:
ylim([0 ceil(max(y(:)))]);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

Geoff on 21 Mar 2012
Organise your data series into columns in a matrix and do a single bar plot.
Assuming a1 and b1 are the same length, do this:
% If a1 and b1 are column-vectors:
bar( [a1 b1] );
% If a1 and b1 are row-vectors:
bar( [a1;b1]' );
If vectors are not the same length, pad them with NaN values
Image Analyst
Image Analyst on 21 Mar 2012
Sorry - it's ColorOrder for plot() but that didn't seem to work for bar(). I had to use colormap() for bar(). See my demo below.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!