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

조회 수: 2 (최근 30일)
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

채택된 답변

Image Analyst
Image Analyst 2018년 10월 1일
Get rid of the first argument to legend():
% Add legend
legend({'Line 1', 'Line2', 'Line 3', 'filled Area'}, 'Location', 'north');
  댓글 수: 3
Image Analyst
Image Analyst 2018년 10월 1일
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')

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by