【问题标题】:move legend position in an empty subplot在空的子图中移动图例位置
【发布时间】:2013-07-24 10:07:14
【问题描述】:

我有一个包含 5 个子图的图形。我声明 subplot(2,3,X) 包括 6 个子图。第 6 个子图是空的。我将把图例移到所有地块的第 6 个空白位置。

这怎么可能?

【问题讨论】:

    标签: matlab plot


    【解决方案1】:

    如果你只想使用标准matlab,你需要子图的句柄,然后你需要它的位置。然后将图例的位置设置为子图的位置。 参考文档:

    注意您可以通过使用“位置”选项将 4 元素位置向量传递给图例函数来设置图例位置。要定义现有图例的位置,请使用 set 函数将 4 元素位置向量分配给“Position”属性。您不能将 Location 选项与 set 函数一起使用

    例如:

    subplot(2,3,1), plot(1:10,2:11)
    myLegend=legend('text1')
    set(myLegend,'Units', 'pixels')
    myOldLegendPos=get(myLegend,'Position')
    hold on
    h=subplot(2,3,6)
    set(h,'Units', 'pixels')
    myPosition=get(h,'Position')
    set(myLegend,'Position',[myPosition(1) myPosition(2) myOldLegendPos(3) myOldLegendPos(4)])
    

    【讨论】:

    • 这个方法对我不起作用。如果我在 R2012b 中复制/粘贴上面的代码,则图例不会出现在图中的任何位置。原因是第二个空白子图“覆盖”了图例。必须在末尾添加行 set(h, 'Visible', 'off') 以使图例可见。
    • 或使用 legend 并将“位置”设置为矢量 [Position_Right_in_pct, Position_Top_in_pct, Horizontal_Stretch Vertical_Stretch] 参见 stackoverflow.com/a/35696869/3494126
    【解决方案2】:

    也许可以试试 File Exchange 中的legendflex,它看起来可以做你想做的事。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-19
      • 2017-09-22
      • 2017-05-24
      • 2020-11-25
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多