在Matlab中使用不同xdata的多列图例

时间:2015-09-05 05:12:40

标签: matlab legend

我想使用legendcolumn.m作为包含不同xdata的子图,如下所示:

  1. 散射(M,N)
  2. scatter(m,t)
  3. plot(f,h)
  4. plot(f,j)
  5. 积(F,K)。
  6. 我的代码是

    "legend_str = ['Individual 1-h averaged SODAR data ';...
                  'Bin-averaged SODAR data            ';...
                  'Fit to Bin-averaged SODAR data     ';...
                  'Quan and Hu (2009)                 ';...
                  'Al-Jiboori et al.(2002)            ';...
                  'Xu et al.(1997)                    ';...
                  'Panofsky et al. (1977)             ']; 
     [legend_h,object_h,plot_h,text_strings]=  columnlegend(2, legend_str);"
    

    在运行中出现此错误: ???指数超过矩阵维度。

    Error in ==> columnlegend at 61
    xdata = get(object_h(numlines+1), 'xdata');
    
    Error in ==> Su_Sv_Sw_self_bin_average_nafiseh_constant_adapted_general at 201
     [legend_h,object_h,plot_h,text_strings]=  columnlegend(2, legend_str);
    

    如果有人帮助我,我将不胜感激。 感谢

2 个答案:

答案 0 :(得分:1)

在您的问题中,您要生成的type的{​​{1}}不明确,即:一组包含5个条目的5个plot或1个subplot。< / p>

在第一种情况下,不能使用函数plot

查看函数的代码,您可以看到它首先生成一个“标准”MatLab columnlegend

legend

然后它使用返回的[legend_h,object_h,plot_h,text_strings] = legend(str); 来修改handles的组件,以便将它们分布在几个列上。

这意味着在legend中绘制的实体数量与图例刺痛中的项目数量之间会出现不匹配。

我是第二种情况,1个轴有5个实体,可以使用函数subplot,但是,你必须将图例字符串定义为columnlegend

cellarray

当然,您首先要修复由@ Benoit_11识别的错误。

实际上,你所得到的错误源于行

legend_str = {'Individual 1-h averaged SODAR data', ...
   'Bin-averaged SODAR data',...
   'Fit to Bin-averaged SODAR data',...
   'Quan and Hu (2009)',...
   'Al-Jiboori et al.(2002)',...
   'Xu et al.(1997)',...
   'Panofsky et al. (1977)'};

numlines = length(str); 定义为legend_str array,其大小为(7x35)而char(不幸的是,你)返回length,这意味着,在您的情况下,max(size(X))

然后将此值用作35,但您的图例字符串只有7行。

另外(但这只是一个建议),最好尝试减少图例字符串的长度,否则它们将跨越所有轴(函数numlines的图例location选项}不包括将图例放在轴外。)

希望这有帮助。

答案 1 :(得分:0)

我决定将我的传奇分为三个部分,并按子图绘制每个部分,因为我有3个子图,每个子图有5个条目。