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!