Add refline (hline) to the legend

17 次查看(过去 30 天)
Hello everybody,
I have a simple (?) question. I have been searching on the web for a while now, but wasn't able to find a satisfying solution. I have several subplots where I added different reflines with the following code:
subplot(2,1,1)
hold on
plot(x1, y1,'--r')
plot(x2, y2,'r','LineWidth',1.5)
hline = refline([0 threshlod_1]);
hline.Color = 'k';
hline.LineStyle = ':';
hline = refline([0 threshold_2]);
hline.Color = 'k';
hline.LineStyle = ':';
legend('data1','data2')
Now I want to add these reflines to the legends of the subplots.
-> legend('data1','data2','threshold_1','threshold_2')
Do you have any idea how this can be done?
Thank you in advance
cheers
Christian

回答(2 个)

Thorsten
Thorsten 2017-4-6
Use handles:
hold on
h(1) = plot(x1, y1,'--r')
h(2) = plot(x2, y2,'r','LineWidth',1.5)
h(3) = refline([0 threshlod_1]);
h(3).Color = 'k';
h(3).LineStyle = ':';
h(4) = refline([0 threshold_2]);
h(4).Color = 'k';
h(4).LineStyle = ':';
legend(h, 'data1','data2','threshold_1','threshold_2')
  1 个评论
Christian
Christian 2017-4-6
That Code gives me the following error:
Structure assignment to non-structure object.
Error in my_script(line 681)
h(3).Color = 'k';

请先登录,再进行评论。


Samuel Anyaso-Samuel
This should work.
subplot(2,1,1)
hold on
plot(x1, y1,'--r')
plot(x2, y2,'r','LineWidth',1.5)
a = refline([0 threshlod_1]);
a.Color = 'k';
a.LineStyle = ':';
a.DisplayName = 'line a'
b = refline([0 threshold_2]);
b.Color = 'k';
b.LineStyle = ':';
b.DisplayName = 'line b'
legend('show')

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by