How to plot multiple boxes with x and y being names
조회 수: 4 (최근 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에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!