Can I display 2 legends while using the plotyy function?

조회 수: 3 (최근 30일)
Brad
Brad 2013년 10월 1일
댓글: Cristian JECU 2015년 3월 17일
I've been asked to add a 2nd legend to an existing M-file where the objective is to display altitude and SV IDs using the plotyy function. I'm working with the following code:
% Plot altitude and SV IDs with NER State Time and add title
figure('Name','Altitude and SV IDs','numbertitle','off');
[AX,H1,H2] = plotyy(State_Time, Alt, State_Time, SV_ID, 'plot');
set(AX, 'Fontsize', 16, 'XTickLabel',num2str(get(gca,'XTick')','%d'));
title(Event_Title,'FontSize', 20);
% Define axes properties
axes(AX(1));
ylabel('Altitude (m)','Fontsize', 20,'fontweight','b');
xlabel('UTC (Sec)','Fontsize', 20,'fontweight','b');
axes(AX(2));
ylabel('SV ID','Fontsize', 20,'fontweight','b');
% Set the markers, markersizes, and linestyles
set(H1,'Marker','o','MarkerSize',15);
set(H2,'Marker','+','MarkerSize',15);
set(H2,'LineStyle','none');
% Add first and 2nd legends for data source
legend(Source, 'Location', 'NortheastOutside');
legend('SV_ID', 'Location', 'EastOutside');
I'm new to using the plotyy function and the problem I'm running into is the displaying of both legends simultaneously.
Is the displaying of both legends tied to the plotyy function itself?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 1일
legend({Source,'SV_ID'}, 'Location', 'NortheastOutside');
  댓글 수: 1
Brad
Brad 2013년 10월 1일
Azzi, this will definitely place both the SV_ID and Source within a single legend. Is there a way to create separate legends for each of these?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Kelly Kearney
Kelly Kearney 2013년 10월 1일
편집: Kelly Kearney 2013년 10월 1일
You can only create one legend per axis (unless you use an outside function like legendflex), but in this case you already have two axes to work with, so:
[ax, h1, h2] = plotyy(1:10, rand(10,1), 1:10, rand(10,1)+10);
leg1 = legend(ax(1), 'thing1', 'Location', 'NortheastOutside');
leg2 = legend(ax(2), 'thing2', 'Location', 'EastOutside');
set(leg2, 'color', 'white');
  댓글 수: 1
Cristian JECU
Cristian JECU 2015년 3월 17일
This solution puts the legend 1, under the lines of the second axes. Is there a solution to bring the legend 1 of the Axes 1 on top?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by