# How to plot multiple boxes with x and y being names

3 views (last 30 days)
Eric on 17 Dec 2015
Answered: Geoff Hayes on 17 Dec 2015
Hi everybody,
Does anyone know how to plot graphs like: http://people.sc.fsu.edu/~jburkardt/m_src/box_plot/clay.png ? With colors being only red, green or the box being blank.
But with text on x and y.
Any help will be much appreciated.
Best regards,
Eric
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Accepted Answer

Geoff Hayes on 17 Dec 2015
Eric - there are probably several ways to do this. One such way is to use fill to create the coloured squares. For example,
n = 5; % number of squares along x and y
% create the figure
figure;
hold on;
axis equal;
% width of square divided by two
sqrWidthBy2 = 0.45;
% set the axis limits
axis([sqrWidthBy2 n+sqrWidthBy2 sqrWidthBy2 n+sqrWidthBy2]);
% array for the fill graphic object handles
sqHandles = zeros(n,n);
% array of colours (white/blank, red, green)
colours = ['w','r','g'];
% loops to create the squares
for x=1:n
for y=1:n
clr = colours(randi(3));
sqHandles(x,y) = fill([x-sqrWidthBy2 x+sqrWidthBy2 x+sqrWidthBy2 x-sqrWidthBy2 x-sqrWidthBy2],...
[y-sqrWidthBy2 y-sqrWidthBy2 y+sqrWidthBy2 y+sqrWidthBy2 y-sqrWidthBy2],clr);
end
end
To "write" text instead of numbers along the x and y axes, we can manipulate the ticks and tick labels of the axis. Something like
set(gca,'YTick',1:n,'YTickLabel',cellstr(char(randi(26,n,1)+64)));
set(gca,'XTick',1:n,'XTickLabel',cellstr(char(randi(26,n,1)+64)));
The above just adds a random character at the integer positions along the x and y axes. You can modify the above to write strings instead of single characters.
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Categories

Find more on Line Plots in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!