How to plot multiple boxes with x and y being names
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Geoff Hayes
2015-12-17
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!