Why my bar chart missing x-axis values?

13 views (last 30 days)
Mohammed
Mohammed on 29 Sep 2014
Commented: Image Analyst on 29 Sep 2014
I am missing x-values on bar plot output also I am not satisfied with the way I coded the program? Any good hints? Thanks!
xValues = [ 0, 0.3333, 0.5, 0.66667,1]; % specifies center of the bars
yValues = [14, 72, 24, 317, 438]; % specifies bar values
width = 0.1;
% Returns lengths of the input vectors
lngxValues = length(xValues);
lngyValues = length(yValues);
% Defining color for each bar
if lngxValues ~= lngyValues
error('Vectors must be the same lengths.')
else
Figure = figure('Name','Vertical Bar Plot'); % Creates a new figure
BarColr = [0.6953 0.1328 0.1328; % Color: FireBrick
0 0.5 0.5; % Color: Teal
0.5430 0 0.5430;
0.5430 0 0.5430;
0.5430 0 0.5430]; % Color: DarkMagenta
for i=1:lngxValues
bar(xValues(i), yValues(i), width, 'FaceColor', BarColr(i,:),'EdgeColor', 'none')
hold on;
end
hold off;
% Adds the values on the top of the bar as text
text(xValues',yValues',num2str(yValues','%i%'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom',...
'FontAngle','italic', 'FontWeight','bold')
end

Answers (1)

Image Analyst
Image Analyst on 29 Sep 2014
When you do bar(xValues(i), yValues(i)) you're only plotting one bar at a time. I suggest you put this after your loop and plot all the values at the same time:
bar(xValues, yValues);
  2 Comments
Mohammed
Mohammed on 29 Sep 2014
I know that but what I am trying is selecting different colors for the bars, if so what happens to defined colored vectors? Will I be able to show the bars with the my selected colors?
Image Analyst
Image Analyst on 29 Sep 2014
See my attached demo, below the picture it makes.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!