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

14 visualizaciones (últimos 30 días)
Teemu Harkonen
Teemu Harkonen el 3 de Abr. de 2020
Editada: darova el 4 de Abr. de 2020
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 comentario
darova
darova el 3 de Abr. de 2020
The question is unclear. Can you make a simple drawing or something?

Iniciar sesión para comentar.

Respuesta aceptada

darova
darova el 3 de Abr. de 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 comentarios
Teemu Harkonen
Teemu Harkonen el 3 de Abr. de 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!
darova
darova el 3 de Abr. de 2020
You are welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by