turning legend off in xline function

How can I turn off legend of xline function being visible on the plot?

1 件のコメント

madhan ravi
madhan ravi 2019 年 4 月 17 日
編集済み: madhan ravi 2019 年 4 月 17 日
Can you share a picture?, I don’t see any legend.

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

回答 (3 件)

Linjun He
Linjun He 2020 年 2 月 22 日

8 投票

You can simply use 'HandleVisibility' as follows:
xline(-1,'--r','HandleVisibility','off');
For those line you want to show the legend, you can use 'DisplayName':
plot(x, y, 'DisplayName', 'This is the legend for this line');
To show the legend:
legend('show');

2 件のコメント

Ann-Cathrin Krause
Ann-Cathrin Krause 2022 年 12 月 17 日
I tried
xline(-1,'--r','HandleVisibility','off');
I wish it worked for me but it didnt.
Adam Danz
Adam Danz 2022 年 12 月 19 日
@Ann-Cathrin Krause, I'm interested in seeing the section of code of plotting code where you're assigning xline and a screenshot of your plot showing the xline in the legend. The short demo below shows that constant line objects with HandleVisibility=off prevents the object from appearing in the legend.
figure()
plot(rand(2), '--')
xline(1.5,'HandleVisibility','off') % won't appear in legend
yline(0.5,'DisplayName', 'YLINE')
legend()

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

Adam Danz
Adam Danz 2019 年 4 月 17 日
編集済み: Adam Danz 2019 年 4 月 23 日

2 投票

Option 1: Use object handles
Specity which objects should appear in the legend and produce the legend at the end after all plotting is complete.
figure
h1 = plot(rand(1,5), rand(1,5));
hold on
h2 = plot(rand(1,5), rand(1,5));
xline(0.5)
legend([h1, h2], {'first', 'second'})
Option 2: Turn off AutoUpdate
If you need to add stuff to the plot after you've created the legend, turn off AutoUpdate before adding the objects.
figure
h1 = plot(rand(1,5), rand(1,5));
hold on
h2 = plot(rand(1,5), rand(1,5));
legend([h1, h2], {'first', 'second'}, 'AutoUpdate', 'off')
xline(0.5)
Sean de Wolski
Sean de Wolski 2019 年 4 月 19 日
編集済み: Sean de Wolski 2019 年 4 月 19 日

1 投票

myxline = xline(10,'r',"Hello")
myxline.Label = ""
myxline.DisplayName = ""
Disable the label and/or DisplayName.

3 件のコメント

Adam Danz
Adam Danz 2019 年 4 月 19 日
You forgot to assign the output
myxline = xline(...)
Also, what does the "Hello" string do? Did you mean this?
xline(10, 'r', 'DisplayName', "Hello")
More importantly, replacing the label and DisplayName with empty strings does not remove that element from the legend.
figure
h = xline(0, 'r', 'DisplayName', 'xline');
h.Label = '';
h.DisplayName = '';
legend() %even if legend is produced before removing label and displayname
190419 100356-Figure 2.jpg
Sean de Wolski
Sean de Wolski 2019 年 4 月 19 日
編集済み: Sean de Wolski 2019 年 4 月 19 日
I had myxline = in my command history, apparently failed to copy, doh!
Hello would be the label for the xline, was just showing how you might have the label or not.
In order to create the legend without the xline() don't call legend() directly, call it only on the graphics objects you want:
h = plot(1:10)
xline(3)
legend(h, "Hello World")
Adam Danz
Adam Danz 2019 年 4 月 19 日
I see. "Hello" labels the line on the plot. That's a nice feature.
The legend handel suggestion matches the 'option 1' in my answer, too.

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

カテゴリ

製品

タグ

質問済み:

2019 年 4 月 17 日

コメント済み:

2022 年 12 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by