How do I control the legend contents when the plot functions are within a for loop?
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Brad
am 12 Jun. 2014
Bearbeitet: Star Strider
am 12 Jun. 2014
I'm running into a problem pertaining to legend control in some legacy code. Specifically, how do I suppress the data2 and data4 entries of the legend (in the attached png file) using the following code:
figure('Name', 'Elapsed Time', 'Position', [350 350 610 250] );
hold on;
for FileNum = 1:num_srcs
h1 = plot(data{FileNum}(:, 9), '-*', 'Color', color1{FileNum}, 'DisplayName', disp_name{FileNum} );
%hold on;
h2 = plot( data2{FileNum}(:, 9), '*', 'Color', color1{FileNum} );
%hold off;
end
%hold off;
legend('location', 'NEO'); % works, but includes the legend entries for data2 and data4
%legend(h1);
%legend(h1, 'location', 'NEO');
%legend(h1, disp_name{FileNum}, 'location', 'NEO');
set(legend, 'FontSize', 20);
H = gca;
set(H, 'YGrid', 'on', 'GridLineStyle', '-');
box on;
The data2 and data4 data series are the points with no lines (far left side of plot).
In reading the MATLAB documentation and reviewing Aravin's question in January 2012, it appeared the solution was to include only h1 in the legend, using the following line of code;
legend(h1, 'location', 'NEO');
This did not produce the desired result. I've also tried re-locating the hold functions and other legend functions - none have worked.
Is it possible the for loop is adversely affecting the legend function?
Here's the link to Aravin's original question in January 2012: http://www.mathworks.com/matlabcentral/answers/25381-how-to-show-partial-legend-in-figure
0 Kommentare
Akzeptierte Antwort
Mischa Kim
am 12 Jun. 2014
Bearbeitet: Mischa Kim
am 12 Jun. 2014
x = 0:0.1:10;
data1 = sin(x);
data2 = cos(x);
data3 = sin(x).^2;
data4 = cos(x).^2;
figure();
p1 = plot(x,data1,'bo-');
hold on
p2 = plot(x,data2,'rx-');
p3 = plot(x,data3,'kd-');
p4 = plot(x,data4,'gs-');
legend([p1,p3],'From data1','From data3');
4 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!