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
댓글 수: 0
답변 (1개)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Bar Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!