【问题标题】:Matlab: variable legend in plotMatlab:图中的变量图例
【发布时间】:2014-06-05 16:04:20
【问题描述】:

我有一个图,其中绘制了可变数量的点(类似于网络的节点)和三个轨迹。当我调用legend 时,我也得到了轨迹的o 符号(对应于节点)。

我知道这是因为legend 函数需要为我需要的每个标签提供一个字符串,但由于我无法“先验”知道节点的总数(因为它是用户定义的参数),是否存在一种指定只有节点被标记为o同时能够正常标记轨迹的方法?

【问题讨论】:

    标签: matlab legend


    【解决方案1】:

    你只需要告诉legend你想要什么。

    轨迹:

    t(1) = plot( xvector1, yvector1, linspecs ...)
    t(2) = plot( xvector2, yvector2, ...)
    ...
    t(n) = plot( xvectorn, yvectorn, ...)
    

    及其标签:

    tLabel{1} = 'trajectory label #1'
    ...
    tLabel{n} = 'trajectory label #n'
    

    节点也一样:

    p(1) = plot( x1, y1, 'o',...)
    p(2) = plot( x2, y2, 'o',...)
    ...
    p(m) = plot( xm, ym, 'o',...)
    
    pLabel{1} = 'node label #1'
    ...
    pLabel{m} = 'node label #m'
    

    最后绘制图例:

    lh = legend( [traj(1:n),p(1:m)] , tLabel{1:n}, pLabel{1:m} );
    

    当然如果所有节点都具有相同的名称和样式,只需使用节点数组的第一项即可:

    lh = legend( [traj(1:n),p(1)] , tLabel{1:n}, pLabel{1} );
    

    【讨论】:

    • 太棒了!不过,我有一个问题,如果用户使用例如 10 个节点,这会不会导致一个非常大的图例框?这些信息不是多余的吗?
    • 当然,看看我的编辑。如果所有节点看起来都一样,只需将第一个条目打印到图例中。
    猜你喜欢
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    相关资源
    最近更新 更多