Why my bar chart missing x-axis values?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Image Analyst
el 29 de Sept. de 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 comentarios
Ver también
Categorías
Más información sobre Bar Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!