
How can i set a legend with plot and fill where i don't plot all curves?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jonatan Klotz
am 1 Okt. 2018
Kommentiert: Jonatan Klotz
am 2 Okt. 2018
clc;
clear all;
%Some example Data
x1=linspace(0,10,1000);
y1=sin(x1);
y2=0.05*x1.^2;
y3=x1/7;
hold on;
grid on;
p1 = plot(x1,y1);
p2 = plot(x1,y2);
p3 = plot(x1,y3);
filler = fill([x1;x1], [y1;y2], 'b', 'edgecolor', 'b', 'edgealpha', 0.1);
%This is what does not work: It does not plot the filled Area with the lines of the plot
legend([p1 p3 filler], {'Linie 1','Linie 3', 'filled Area'});
% This is what i found if i got the 'patch' in the legend and let the FaceAlpha as in the Area
% PatchInLegend = findobj(icons, 'type', 'patch');
% if ~isempty(PatchInLegend)
% set(PatchInLegend(1), 'FaceAlpha', 0.4);
% end
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 1 Okt. 2018
Get rid of the first argument to legend():
% Add legend
legend({'Line 1', 'Line2', 'Line 3', 'filled Area'}, 'Location', 'north');

3 Kommentare
Image Analyst
am 1 Okt. 2018
Set the handleVisibility to 'off' and leave line2 out of the legend argument list:
%Some example Data
x1=linspace(0,10,1000);
y1=sin(x1);
y2=0.05*x1.^2;
y3=x1/7;
hold on;
grid on;
p1 = plot(x1,y1, 'r-', 'LineWidth', 2);
p2 = plot(x1,y2, 'g-', 'LineWidth', 3);
p3 = plot(x1,y3, 'b-', 'LineWidth', 2);
filler = fill([x1;x1], [y1;y2], 'b', 'edgecolor', 'b', 'edgealpha', 0.1);
p2.HandleVisibility = 'off';
% Add legend
hLegend = legend({'Line 1', 'Line 3', 'filled Area'}, 'Location', 'north')

Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!