Legend of a patch-object with a line in the center

조회 수: 13 (최근 30일)
Teemu Harkonen
Teemu Harkonen 2020년 4월 3일
편집: darova 2020년 4월 4일
Hi,
I am looking for a way to have a custom legend entry for a filled area plot along with a line plot where the individual legend entries would be combined into one entry. The single entry should have the line marker overlaid on top of the box of the filled area legend. Is this possible?
x = [0 1];
y = [1 0];
xArea = [x, fliplr(x)];
yArea = [y + 1, fliplr( y - 1 )];
figure();
hold on;
hp = plot( x, y);
hp.LineWidth = 2;
hp.HandleVisibility = 'off';
hf = fill( xArea, yArea, [0, 0.4470, 0.7410]);
hf.FaceAlpha = 0.25;
hf.LineStyle = 'none';
hLeg = legend('Single legend entry');
hLeg.FontSize = 30;
Result of the example code:
Desired look of the legend entry:
  댓글 수: 1
darova
darova 2020년 4월 3일
The question is unclear. Can you make a simple drawing or something?

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

채택된 답변

darova
darova 2020년 4월 3일
What about this? Too complicated? No?
clc,clear
cla
[X,Y,Z] = peaks(5);
p = surf2patch(X,Y,Z);
p1 = patch(p,'facecolor','g');
h = legend('ha');
h1 = get(h,'children');
x = get(h1(1),'xdata');
y = get(h1(1),'ydata');
% c = get(h1(1),'faceVertexCData');
x = [x; x(1); x(end)];
y = [y; mean(y); mean(y)];
c = [ repmat([0 1 0],[4 1]); repmat([1 0 0],[2,1]) ];
ff = [1 2 3 4; 5 6 6 6];
set(h1(1),'vertices',[x y],...
'faces',ff,...
'faceVertexCData', c,...
'edgecolor','flat')
  댓글 수: 2
Teemu Harkonen
Teemu Harkonen 2020년 4월 3일
Thank you for the answer! I believe this approach might work in versions earlier than 2014b where they made significant changes to the graphics objects.
Using your solution as a template, I was able to do this in MATLAB 2019b by:
x = [0 1];
y = [1 0];
xArea = [x, fliplr(x)];
yArea = [y + 1, fliplr( y - 1 )];
blue = [0, 0.4470, 0.7410];
figure();
hold on;
hp = plot( x, y);
hp.LineWidth = 2;
hp.HandleVisibility = 'off';
hf = fill( xArea, yArea, blue);
hf.FaceAlpha = 0.25;
hf.EdgeColor = 'none';
[hLeg, legObj] = legend('Single legend entry $\hspace{1cm}$', 'interpreter', 'latex');
hLeg.FontSize = 30; % Without these (hspace and fontsize) the legend box size is too small
texts = legObj(1);
p = legObj(2);
p.FaceAlpha = 0.25;
p.LineWidth = 2;
texts.FontSize = 30;
x = get( p,'xdata');
y = get( p,'ydata');
x = [x; x(1); x(end)];
y = [y; mean(y); mean(y)];
c = [ repmat([1, 1, 1],[4 1]); repmat(blue,[2,1]) ];
ff = [1 2 3 4; 5 6 6 6];
set( p,'vertices',[x y],...
'faces',ff,...
'faceVertexCData', c,...
'edgecolor','flat')
Thank you for the help!
darova
darova 2020년 4월 3일
You are welcome!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by