create a bar graph with colors indicator in matlab

Here is an example code to create a bar graph with colors indicators in MATLAB:

main.m
% Simulated data
x = 1:10;
y = [50 35 20 18 12 9 9 8 7 4];

% Specify colors for each bar
colors = [0.5 0.5 0.5; 0.25 0.25 0.75; 0.75 0.25 0.25; 0.25 0.75 0.25; 0.75 0.75 0.25; 0.25 0.75 0.75; 0.75 0.25 0.75; 0.75 0.75 0.75; 0.5 0.5 1; 1 0.5 0.5];

% Create bar graph
bar(x, y, 'FaceColor', 'flat')

% Set custom colors for each bar
for i = 1:length(colors)
    cdata(i,:) = repmat(colors(i,:),length(x),1);
end
set(gca, 'ColorOrder', colors, 'Color', 'w')
set(bar(x, y), 'CData', cdata)

% Set axes properties
xlabel('X')
ylabel('Y')
title('Bar Graph with Color Indicators')
legend('Bar')

% Display grid lines
grid on
620 chars
26 lines

Here, x and y are the data points for the bar graph. The colors variable specifies the custom colors for each bar in the graph. bar function is used to create a bar graph with flat colors. set function is used to set custom colors for each bar in the graph. The xlabel, ylabel, title, and legend functions are used to set the axes properties and display the legend. The grid on function is used to display the grid lines in the graph.

gistlibby LogSnag