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

14 visualizzazioni (ultimi 30 giorni)
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:

Risposta accettata

darova
darova il 3 Apr 2020
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 Commenti
Teemu Harkonen
Teemu Harkonen il 3 Apr 2020
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!

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by