フィルターのクリア

Why my bar chart missing x-axis values?

8 ビュー (過去 30 日間)
Mohammed
Mohammed 2014 年 9 月 29 日
コメント済み: Image Analyst 2014 年 9 月 29 日
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 Center および File ExchangeBar Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by