Add refline (hline) to the legend

조회 수: 14 (최근 30일)
Christian
Christian 2017년 4월 6일
답변: Samuel Anyaso-Samuel 2018년 3월 5일
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
Samuel Anyaso-Samuel 2018년 3월 5일
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