【问题标题】:Controlling subplot font properties at once but independently一次但独立地控制子图字体属性
【发布时间】:2018-11-21 12:24:25
【问题描述】:

我正在尝试以一种独立于其他字体属性的方式控制子图标题的属性,同时使用乳胶作为解释器(我认为最后一部分不相关,但以防万一)。这是一个示例代码:

% Figure handle
fig1 = figure;

% Subplot 1
subplot(2,1,1)
plot(rand(100,1))
xlabel('$x$ label')
ylabel('$y$ label')
title('First subplot')

% Subplot 2
subplot(2,1,2)
plot(rand(100,1))
xlabel('$x$ label')
ylabel('$y$ label')
title('Second subplot')

% Setting global properties
set(findall(fig1,'-property','FontSize'),'FontSize',14)
set(findall(fig1,'-property','Interpreter'),'Interpreter','Latex')
set(findall(fig1,'-property','TickLabelInterpreter'),'TickLabelInterpreter','Latex')

当我这样做时,我可以设置轴标签、刻度标签和子图标题的大小和解释器。这使得标题具有与其他对象相同的样式。

有没有办法独立控制标题属性,例如使它们稍微更大和更粗,以便与轴标签轻松区分开来?

【问题讨论】:

    标签: matlab matlab-figure


    【解决方案1】:

    如果只是想让标题变大,可以在调用title命令时设置:

    title('First subplot', 'FontSize', 14, 'FontWeight', 'bold')
    

    如果您想更好地控制单个对象的字体大小,则必须存储轴句柄(在创建子图时创建):

    ax1 = subplot(211)
    ax2 = subplot(212)
    
    % set the properties of the title:
    ax1.Title.FontSize = 14;
    
    % set the properties of the XAxis:
    ax1.XAxis.FontSize = 7;
    

    要查看可以更改的设置,只需在命令窗口中调用句柄,这将为您提供更多详细信息:

    >> ax1.Title
    
    ans = 
    
      Text (First subplot) with properties:
    
                     String: 'First subplot'
                   FontSize: 14
                 FontWeight: 'bold'
                   FontName: 'Helvetica'
                      Color: [0 0 0]
        HorizontalAlignment: 'center'
                   Position: [50.0001 1.0139 0]
                      Units: 'data'
    

    如果要在图中设置不同轴(子图)的标题属性,可以将轴存储在元胞数组中:

    ax = {subplot(211), subplot(212)};
    
    plot(ax{1}, rand(100,1));
    plot(ax{2}, rand(100,1));
    
    for i=1:numel(ax)
        ax{i}.Title.Fontsize = 14;
    end
    

    【讨论】:

    • 我知道title 命令的使用。但这需要单独设置每个title(或轴句柄)的属性。我试图找到一种方法来一次更改 all 标题的属性,就像我在示例中对其他属性所做的那样。有什么想法吗?
    • 我对您的新解决方案不满意。我收到错误No public property Fontsize exists for class matlab.graphics.primitive.Text.。也许这个 Title.Fontsize 属性并不存在于所有 Matlab 版本中?我正在使用 2015b
    • @Looper 哦忘了大写我想,它适用于ax{i}.Title.FontSize吗?
    • 你说得对,需要大写的 S。但是这个解决方案仍然有很多问题:(1)它似乎不能很好地在同一轴上绘制多个图,即使在指定hold onplot(ax{i}, ....)之后,它也只绘制最后一个plot轴,除了你得到所有情节的最后一个(很奇怪,我知道)。 (2) 要使标题、图例和轴标签正常工作,您需要为它们中的每个指定轴,即title(ax{1},'First') 等,因此自动化属性的价值不大结束。
    【解决方案2】:

    set指令可以应用于图形句柄数组,所以如果你想修改所有标题的属性,只需将它们的句柄先收集到一个数组中,然后在句柄数组上使用set命令.

    所以在你的例子中,替换你的

    % ...
    title('First subplot')
    % ...
    title('Second subplot')
    % ...
    

    作者:

    % ...
    ht(1) = title('First subplot')
    % ...
    ht(2) = title('Second subplot')
    % ...
    

    现在,您的标题拥有一组句柄 ht。现在批量修改它们,不修改任何其他内容:

    set( ht , 'FontSize',18, 'FontWeight','bold')
    

    同样,您可以重新组合其他对象的句柄以一次性分配它们的属性:

    % build a collection of xlabel array
    hxlabel = [hax(1).XLabel hax(2).XLabel] ;
    % Set their label and interpreter all at once
    set( hxlabel , 'String' , '$x$ label' , 'Interpreter','Latex' )
    

    这会将相同的xlabel 应用于所有子图,并同时将它们的解释器设置为乳胶。

    同样的推理可以应用于ylabel,或许多对象的任何其他公共属性。

    【讨论】:

      【解决方案3】:

      对于它的价值,我必须通过设置以下“全局”属性(放置在上面示例的末尾)来解决这个问题:

      % Setting global properties
      set(findall(fig1,'-property','Interpreter'),'Interpreter','latex')
      set(findall(fig1,'-property','TickLabelInterpreter'),'TickLabelInterpreter','latex')
      set(findall(fig1,'-property','Title'),'FontSize',14)
      set(findall(fig1.Children,'-property','TitleFontSizeMultiplier'),'TitleFontSizeMultiplier',1.8)
      

      有几点需要注意。图形句柄中的属性Children.TitleFontSizeMultiplier 将您拥有的任何内容缩放为FontSize。但是,不能将规范 FontSize 放在 Interpreter 之前,因为这似乎会锁定任何进一步的字体大小规范。

      如果您在使用latex 解释器时想要粗体,您需要直接在标题中指定:title('\textbf{First subplot}')。在normalbold 之间更改属性Children.TitleFontWeight 似乎没有任何效果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-06
        • 1970-01-01
        • 1970-01-01
        • 2021-02-15
        • 1970-01-01
        • 1970-01-01
        • 2013-11-02
        • 1970-01-01
        相关资源
        最近更新 更多