フィルターのクリア

group of lines with the same color (legend)

97 ビュー (過去 30 日間)
maya
maya 2015 年 12 月 11 日
i have two groups of lines, one group in blue (4 lines) and the second in red (3 lines) at the same plot. i want to create a legend with two labels - one for the blue group and the second for the red group, i can't find a function to do so. the only thing that i manage to do is to write 7 labels, and then i get label for each line but it looks stupid. i want to have only two labels for each group.
thanks
  1 件のコメント
Socrates - Athanasios Kotronakos
Socrates - Athanasios Kotronakos 2022 年 5 月 30 日
Hey! Could you share how you did it? Thank you

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

回答 (2 件)

Walter Roberson
Walter Roberson 2015 年 12 月 11 日
You can record the handles of the lines, and only pass in one of the red group and one of the blue group to legend. For example,
h1 = plot(x, first_y, 'b'); %first_y has 4 columns so h1 is length 4
h2 = plot(x, second_y, 'r'); %second_y has 3 columns so h2 is length 3
legend([h1(1), h2(1)], 'Blue Meanies', 'Red Rovers')
  1 件のコメント
Benjamin Schafer
Benjamin Schafer 2019 年 1 月 31 日
Is this still working with later versions of Matlab? The new legend command in Matlab 2018 (at least when I am runing into it) seems to want to be smart and list all lines no matter what I do with the handles. I found some gitHub threads - but man the new legend command is totally different than my workflow before which was very much like this example.

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


Taniel Winner
Taniel Winner 2021 年 7 月 24 日
Use this function I found extremely helpful and easy to use in my code.
  1 件のコメント
Adam Danz
Adam Danz 2021 年 8 月 31 日
Thanks, Taniel. I've edited your comment to convert the URL to a link.

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

カテゴリ

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