【问题标题】:MATLAB: Trying to add shared xlabel,ylabel in 3x2 subplotMATLAB:尝试在 3x2 子图中添加共享 xlabel、ylabel
【发布时间】:2015-06-11 21:33:59
【问题描述】:

正如标题所说,我很乐意做的是相当直截了当。我有一个 m=3,n=2 子图的网格。它们代表了测量相同参数的 6 个不同实验的图表。我想在六个子图的边界上有一个 x 标签和一个 y 标签。不幸的是,到目前为止,我还没有找到一种简单的方法来做到这一点。 (xlabel 只是在最后一个活动子图下放置一个 xlabel)。有谁知道这是怎么做到的?

哦,我将如何在带有度数符号的标签中显示摄氏度?(小圆圈...)

【问题讨论】:

    标签: matlab plot graphing


    【解决方案1】:

    您可以使用mtit 在子图周围创建一个不可见的轴。 mtit 返回该轴的句柄,然后您可以为其创建 xlabel 和 ylabel。

    例子:

    % create sample data
    my_data = arrayfun(@(x)rand(10, 2) + repmat([x, 0], 10, 1), 1:6, 'UniformOutput', 0);
    
    figure;
    clf
    ah = gobjects(6, 1); % use zeros if using an old version of MATLAB
    % plot data
    for ii = 1:6
        ah(ii) = subplot(3, 2, ii);
        plot(1:10, my_data{ii}(:, 1));
        hold on
        plot(1:10, my_data{ii}(:, 2));
    end
    % link axes to have same ranges
    max_data = max(cellfun(@(x) max(x(:)), my_data));
    min_data = min(cellfun(@(x) min(x(:)), my_data));
    linkaxes(ah, 'xy')
    ylim([min_data, max_data])
    
    % Create invisible large axes with title (title could be empty)
    hh = mtit('Cool experiment');
    %set(gcf, 'currentAxes', hh.ah)
    % make ylabels
    ylh = ylabel(hh.ah, 'Temperature [°C]');
    set(ylh, 'Visible', 'On')
    xlh = xlabel(hh.ah, 'x label');
    set(xlh, 'Visible', 'On')
    

    这将产生一个像这样的图形:

    【讨论】:

    • 谢谢,这正是我想要的。
    【解决方案2】:

    我不知道您在为每个子图设置 xlabelylabel 时遇到了什么错误。

    我也不确定我是否理解了问题。

    以下代码生成 3x2 子图,每个子图都有 xlabelylabel

    在第一种情况下,每个子图都有不同的 xlabelylabel 字符串。

    在第二个中,为所有 subplos 设置了相同的 xlabelylabel

    要将“°”符号添加到标签中,这样定义char 变量就足够了:

    c='°'
    

    然后使用sprintfxlabelylabel 生成字符串。

    a=randi(100,6,20)

    figure
    
    % Each subplot with its own xlabel and ylabel
    for i=1:6
       hs(i)=subplot(3,2,i);
       plot(a(i,:))
       c='°'
       str=sprintf('Temp [C%c]',c)
       xlabel([str ' ' num2str(i)])
       ylabel(['Subplot ' num2str(i)])
       grid on
    end
    
    figure
    
    % The same xlabel and ylabel for all the subplot
    c='°';
    x_label_str=sprintf('Temp [C%c]',c)
    y_label_str='Same for all'
    
    for i=1:6
       hs(i)=subplot(3,2,i);
       plot(a(i,:))
       xlabel(x_label_str)
       ylabel(y_label_str)
       grid on
    end
    

    图 1:每个子图的不同 xlabel、ylabel

    图 2:每个子图的相同 xlabel、ylabel

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      我认为您可以从以下位置使用超级标签: http://www.mathworks.com/matlabcentral/fileexchange/7772-suplabel

      【讨论】:

        猜你喜欢
        • 2013-04-15
        • 2017-07-02
        • 1970-01-01
        • 2019-02-19
        • 2014-06-23
        • 1970-01-01
        • 2020-11-08
        • 1970-01-01
        • 2015-07-19
        相关资源
        最近更新 更多