poster
2019-12-10, 20:48
我有以下脚本来最终绘制4 x 2子图:
files = getAllFiles('preliminaries'); n = size(files); cases = cell(1, n); m = cell(1, n); for i = 1:1:n S = load(files{i}); cases{i} = retransmission_distribution(S); c = size(cases{i}); m{1,i} = cell(1, c(2)); %figure(i); str_size = size(files{i}); title_str = files{i}(5:str_size(2) - 4); title_str = strrep(title_str, '_', ' '); %title(title_str); for j = 1:1:c(2) [x, y] = hist(cases{i}{1,j}); m{1,i}{1,j} = [x; int32(y)]; % subplot(4, 2, j); % xlabel('Number of Retransmissions'); % ylabel('Number of Occurrences'); % bar(y, x, 'histc'); end end 但是,按照命令序列的当前顺序,即使没有注释,标题和轴标签也会存在一段时间,然后再被擦除。我希望该图具有其自己的标题,每个子图都有其自己的轴标签。最简单的解决方法是什么?
回答:
对于轴标签, Matt是正确的 (https://stackoverflow.com/questions/3454369/matlab-subplot-title-and-axes-labels/3455153#3455153) ,必须在调用BAR (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/bar.html) 之后放置它们。这将解决一个轴标签问题。但是,您可能会注意到,如果您的y轴标签太长,则最终可能会被彼此覆盖。您有几种选择可以解决此问题。首先,您可以在致电YLABEL时 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlabel.html)调整字体大小:
ylabel('Number of Occurrences','FontSize',7); 第二,您可以通过使用字符串 (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f2-47856.html#f2-38312)的单元格数组 (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f2-47856.html#f2-38312)而不是单个字符串,将一个长标签转换为多行标签:
ylabel({'Number of' 'Occurrences'}); 要在整个图形上添加标题,最好的选择可能是制作一个UICONTROL (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/uicontrol.html)静态文本对象并调整其位置,使其放置在图形顶部附近。您可以先获取图形的大小和位置,以帮助您将文本框放在顶部和中央附近:
figureSize = get(gcf,'Position'); uicontrol('Style','text',... 'String','My title',... 'Position',[(figureSize(3)-100)/2 figureSize(4)-25 100 25],... 'BackgroundColor',get(gcf,'Color')); 这将创建一个静态文本框,该文本框的宽度为100像素,高度为25像素,位于图的顶部中央,并且背景颜色与图相同。
更多&回答... (https://stackoverflow.com/questions/3454369)
files = getAllFiles('preliminaries'); n = size(files); cases = cell(1, n); m = cell(1, n); for i = 1:1:n S = load(files{i}); cases{i} = retransmission_distribution(S); c = size(cases{i}); m{1,i} = cell(1, c(2)); %figure(i); str_size = size(files{i}); title_str = files{i}(5:str_size(2) - 4); title_str = strrep(title_str, '_', ' '); %title(title_str); for j = 1:1:c(2) [x, y] = hist(cases{i}{1,j}); m{1,i}{1,j} = [x; int32(y)]; % subplot(4, 2, j); % xlabel('Number of Retransmissions'); % ylabel('Number of Occurrences'); % bar(y, x, 'histc'); end end 但是,按照命令序列的当前顺序,即使没有注释,标题和轴标签也会存在一段时间,然后再被擦除。我希望该图具有其自己的标题,每个子图都有其自己的轴标签。最简单的解决方法是什么?
回答:
对于轴标签, Matt是正确的 (https://stackoverflow.com/questions/3454369/matlab-subplot-title-and-axes-labels/3455153#3455153) ,必须在调用BAR (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/bar.html) 之后放置它们。这将解决一个轴标签问题。但是,您可能会注意到,如果您的y轴标签太长,则最终可能会被彼此覆盖。您有几种选择可以解决此问题。首先,您可以在致电YLABEL时 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlabel.html)调整字体大小:
ylabel('Number of Occurrences','FontSize',7); 第二,您可以通过使用字符串 (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f2-47856.html#f2-38312)的单元格数组 (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f2-47856.html#f2-38312)而不是单个字符串,将一个长标签转换为多行标签:
ylabel({'Number of' 'Occurrences'}); 要在整个图形上添加标题,最好的选择可能是制作一个UICONTROL (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/uicontrol.html)静态文本对象并调整其位置,使其放置在图形顶部附近。您可以先获取图形的大小和位置,以帮助您将文本框放在顶部和中央附近:
figureSize = get(gcf,'Position'); uicontrol('Style','text',... 'String','My title',... 'Position',[(figureSize(3)-100)/2 figureSize(4)-25 100 25],... 'BackgroundColor',get(gcf,'Color')); 这将创建一个静态文本框,该文本框的宽度为100像素,高度为25像素,位于图的顶部中央,并且背景颜色与图相同。
更多&回答... (https://stackoverflow.com/questions/3454369)