Keeping plot legend in foreground

21 ビュー (過去 30 日間)
Matt J
Matt J 2012 年 10 月 19 日
回答済み: Torben Knudsen 2022 年 9 月 28 日
When I add a line object to my plot, the legend is for some reason pushed into the background behind the plot axis, where it is hidden. Why does this happen and how do I bring it into the foreground again?
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 19 日
Can you post your code?

サインインしてコメントする。

採用された回答

Matt Fig
Matt Fig 2012 年 10 月 19 日
編集済み: Matt Fig 2012 年 10 月 19 日
I cannot reproduce this with:
plot(1:10)
legend('plot')
line([5 5],[0 10])
So I want to ask what your code looks like. If you only have the one plot axes and the legend (no uicontrols or other figure children), then this will bring the legend back:
set(gcf,'children',flipud(get(gcf,'children')))
If you have other figure children, the idea is the same, you will just have to be more careful about the rearrangement.

その他の回答 (2 件)

Andrianna Ayiotis
Andrianna Ayiotis 2018 年 12 月 12 日
編集済み: Andrianna Ayiotis 2018 年 12 月 12 日
I had a similar problem recently but with my legend hiding behind one of my 6 subplots. When I used
set(gcf,'children',flipud(get(gcf,'children')))
my legend would then hide behind other the other subplots if I dragged it up there.
The MATLAB documentation says that the 'children' property of gcf cannot be used to add or delete graphical arrays in the group, only to reorder them. Here is a general way to make sure the legend is in front of all the other plots but be careful with this method if you have overlapping subplots that need to be stacked in a certain order.
ax = get(gcf,'children');
ind = find(isgraphics(ax,'Legend'));
set(gcf,'children',ax([ind:end,1:ind-1]))

Torben Knudsen
Torben Knudsen 2022 年 9 月 28 日
I have solved a similar problem with
set(lh,'Color','white');
where lh is the legend handle

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by