How can i set a legend with plot and fill where i don't plot all curves?

2 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Image Analyst
Image Analyst le 1 Oct 2018
Get rid of the first argument to legend():
% Add legend
legend({'Line 1', 'Line2', 'Line 3', 'filled Area'}, 'Location', 'north');
  3 commentaires
Image Analyst
Image Analyst le 1 Oct 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')
Jonatan  Klotz
Jonatan Klotz le 2 Oct 2018
Ur the Best. Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by