How can I delete legends from a plot or subplot?

497 ビュー (過去 30 日間)
Deniz Baturay
Deniz Baturay 2016 年 3 月 27 日
編集済み: MathWorks Support Team 2024 年 1 月 3 日
When I plot the model returned by the "fitlm" function, a legend is automatically added to the plot. I am creating multiple subplots, and would like to remove repetitive legends. How can I do this?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2024 年 1 月 2 日
編集済み: MathWorks Support Team 2024 年 1 月 3 日
Below are two ways to remove a legend from a plot:
  1. Use the "findobj" function to find the legend(s) and the "delete" function to delete them: 
    % Find and delete the legend
    lgd = findobj('type', 'legend')
    delete(lgd)
    If you have multiple plots on the same figure (and thus multiple legends), the above will delete all of the legends for all of the plots. To only remove specific legends, you can delete the legend at a specific index:\n
    delete(lgd(idx)) % where idx is the index of the legend(s) to delete
  2. If you do not want to delete the legend, you can turn off the legend's visibility:
    set(lgd(idx), 'visible', 'off')
  3 件のコメント
Deniz Baturay
Deniz Baturay 2016 年 3 月 27 日
I changed 'type' as 'tag' and it works. Thanks.
RUBINEI DORNELES MACHADO
RUBINEI DORNELES MACHADO 2020 年 5 月 3 日
Hi ... if you need delete specific legend in position x:
lgd.String(x)=[ ];
;)

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

その他の回答 (1 件)

MHN
MHN 2016 年 3 月 27 日
編集済み: MHN 2016 年 3 月 27 日
Open your figure and type in the command line:
legend('hide')
If you have many figures, put this in a loop and save the figure after hiding the legend.
  1 件のコメント
Deniz Baturay
Deniz Baturay 2016 年 3 月 27 日
Thanks for the answer.

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

カテゴリ

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