Why my bar chart missing x-axis values?

8 次查看(过去 30 天)
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

回答(1 个)

Image Analyst
Image Analyst 2014-9-29
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 个评论
Mohammed
Mohammed 2014-9-29
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 2014-9-29
See my attached demo, below the picture it makes.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Bar Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by