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

2 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Image Analyst
Image Analyst el 1 de Oct. de 2018
Get rid of the first argument to legend():
% Add legend
legend({'Line 1', 'Line2', 'Line 3', 'filled Area'}, 'Location', 'north');
  3 comentarios
Image Analyst
Image Analyst el 1 de Oct. de 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')

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Legend en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by