Clear Filters
Clear Filters

How to plot multiple boxes with x and y being names

3 views (last 30 days)
Hi everybody,
Does anyone know how to plot graphs like: ? 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,

Accepted Answer

Geoff Hayes
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
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);
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
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.

More Answers (0)


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!