MATLAB 子图标题和轴标签

发布于 2024-09-13 23:16:33 字数 799 浏览 3 评论 0 原文

我有以下脚本来最终绘制 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

但是,按照我当前的命令序列顺序,即使未注释它们,标题和轴标签也会在被删除之前存在一段时间。我希望该图有自己的标题,每个子图都有自己的轴标签。修复它最简单的方法是什么?

I have the following script to ultimately plot a 4 by 2 subplot:

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

However, with the current order of command sequence I have, even with them uncommented, the title and axis labels were present for a time before being erased. I want the figure to have its own title, with each subplot having its own axis labels. What's the easiest way to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨落□心尘 2024-09-20 23:16:33

对于轴标签,Matt 关于它们必须放置在调用 BAR 之后 。这将解决一个轴标签问题。但是,您可能会注意到,如果 y 轴标签太长,它们最终可能会被相互覆盖。您有几个选项可以解决此问题。首先,您可以将调用中的字体大小调整为 YLABEL< /a>:

ylabel('Number of Occurrences','FontSize',7);

其次,您可以使用 字符串单元格数组 而不是单个字符串:

ylabel({'Number of' 'Occurrences'});

要向整个图形添加标题,最好的选择可能是制作 UICONTROL 静态文本对象并调整其位置,使其放置在顶部附近该图的。您可以先获取图形的大小和位置,以帮助您将文本框放置在顶部和中心附近:

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 像素的静态文本框,放置在图形顶部的中心并具有与图相同的背景颜色。

For the axis labels, Matt is correct about them having to be placed after the call to BAR. That will take care of one axis label problem. However, you'll likely notice that your y-axis labels in particular may end up being written over one another if they are too long. You have a couple of options to fix this. First, you can adjust the font size in your call to YLABEL:

ylabel('Number of Occurrences','FontSize',7);

Second, you can convert one long label into a multi-line label by using a cell array of strings instead of just a single string:

ylabel({'Number of' 'Occurrences'});

To add a title to the entire figure, the best option is probably to make a UICONTROL static text object and adjust its position so it is placed near the top of the figure. You can get the size and the position of the figure first to help you place the text box near the top and center:

figureSize = get(gcf,'Position');
uicontrol('Style','text',...
          'String','My title',...
          'Position',[(figureSize(3)-100)/2 figureSize(4)-25 100 25],...
          'BackgroundColor',get(gcf,'Color'));

This will create a static text box of width 100 pixels and height 25 pixels placed at the center of the top of the figure and with the same background color as the figure.

番薯 2024-09-20 23:16:33

suptitle 就是您要找的内容。

它将标题置于所有图上方的中心。

SUPTITLE Puts a title above all subplots.
    SUPTITLE('text') adds text to the top of the figure
    above all subplots (a "super title"). Use this function
    after all subplot commands.

suptitle is what you are looking for.

It places the title centered above all plots.

SUPTITLE Puts a title above all subplots.
    SUPTITLE('text') adds text to the top of the figure
    above all subplots (a "super title"). Use this function
    after all subplot commands.
〆一缕阳光ご 2024-09-20 23:16:33

这是我不久前在 MATLAB 交流论坛上看到的一个解决方案,它对我来说非常有效。创建图形后,执行以下命令序列:

set(gcf,'NextPlot','add');
axes;
h = title('Intended Figure Title');
set(gca,'Visible','off');
set(h,'Visible','on');

Here's a solution I saw on a MATLAB exchange forum a while back and that worked for me pretty well. After creating the figure, execute the following sequence of commands:

set(gcf,'NextPlot','add');
axes;
h = title('Intended Figure Title');
set(gca,'Visible','off');
set(h,'Visible','on');
苏佲洛 2024-09-20 23:16:33

据我所知,标题函数将文本相对于一组轴放置,因此不存在图形标题之类的东西。可能的解决方法包括仅对放置良好的子图使用标题(例如第一个子图或顶行的中间子图),或者在您想要标题的位置手动创建一组轴。

至于轴标签,请尝试将标签命令放在条形命令之后。

As far as I know the title function places text relative to a set of axes, so there is no such thing as a figure title. Possible workarounds include using title for a well placed subplot only (such as the first one, or the middle one of the top row), or manually creating a set of axes in the location where you want your title.

As for the axis labels, try putting the label commands after the bar command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文