Drexel dragonThe Math ForumDonate to the Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Coloring Select Bars in a Histogram or Bar Plot
Replies: 5   Last Post: May 29, 2013 9:10 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 12,969
Registered: 12/26/06
Re: Coloring Select Bars in a Histogram or Bar Plot
Posted: May 29, 2013 8:57 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Run my demo:

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

% Demo macro plot a bar chart and give a different color to each bar.
% Also plots the value of the bar above the bar.
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.
fontSize = 30;
format compact

% Ask user for the number of bars.
defaultValue = 7;
titleBar = 'Enter an integer value';
userPrompt = 'Enter the number of bars';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
integerValue = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);

% Define sample data in the range 20-80.
x = 1 : integerValue;
y = 20 + 80 * rand(integerValue)
numberOfBars = length(y);

button = menu('Use which colormap?', 'Custom', 'Random', 'Jet', 'Hot', 'Lines');
if button == 1
% Make up a custom colormap specifying the color for each bar series.
barColorMap(1,:) = [.2 .71 .3]; % Green Color for segment 1.
barColorMap(2,:) = [.25 .55 .79]; % Blue Color for segment 2.
barColorMap(3,:) = [.9 .1 .14]; % Red Color for segment 3.
barColorMap(4,:) = [.9 .9 .14]; % Yellow Color for segment 4.
% I have not defined any more than 4 colors in this demo.
% For any number of bars beyond 4, just make up random colors.
if numberOfBars > 4
barColorMap(5:numberOfBars, 1:3) = rand(numberOfBars-4, 3);
elseif button == 2
% Example of using colormap with random colors
barColorMap = rand(numberOfBars, 3);
elseif button == 3
% Example of using pre-defined jet colormap
barColorMap = jet(numberOfBars);
elseif button == 4
% Example of using pre-defined Hot colormap
barColorMap = hot(numberOfBars);
% Example of using pre-defined lines colormap
barColorMap = lines(numberOfBars);

% 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,:));
% Place text atop the bar
barTopper = sprintf('y(%d) = %.3f', x(b), y(b));
text(x(b)-0.2, y(b)+3, barTopper, 'FontSize', 15);
hold on;

% Fancy up the graph.
grid on;
caption = sprintf('Data plotted in %d barseries, each with a different color', length(y));
title(caption, 'FontSize', fontSize);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
% Restore the x tick marks.
set(gca, 'XTickMode', 'Auto');
% set(gca, 'XTickLabels', xTickLabels);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off');

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum 1994-2015. All Rights Reserved.