Borrar filtros
Borrar filtros

How can you have two different legend charts on the same plots with different titles?

212 visualizaciones (últimos 30 días)
I have a a figure where I am trying to plot 6 different graphs. 3 of these graphs belong to a particular subset which represent components x, y, and z. The other 3 below to another subset which also contain x,y , and z components. I would like to have two differnet legend plots on the same graph with a differnt title where the titles are 'Subset 1' and 'Subset 2'.
  3 comentarios
Alessandro Maria Laspina
Alessandro Maria Laspina el 3 de Mayo de 2021
I haven't found a solution that also lets you add titles to the different legend boxes.
Dyuman Joshi
Dyuman Joshi el 3 de Mayo de 2021
https://in.mathworks.com/matlabcentral/answers/430791-how-to-add-a-second-legend-box-to-a-figure-without-new-plots#answer_382499

Iniciar sesión para comentar.

Respuesta aceptada

Divya Gaddipati
Divya Gaddipati el 13 de Mayo de 2021
You can try the following approach:
  • You will need to have two plots per line (with each plot using one property) if you need two legends.
  • Then add the legend for the first property by only using the lines that are representative of the first property.
  • You can then use the 'copyobj' function to copy the legend and its axis.
  • Then in the new axis you can change the legend in that handle to be the legend of only those lines that are representative of the second property.
  • Make sure that both the legends are outside the plot. You can make one of the axis invisible if needed.
figure
ah = axes;
x = linspace(0, 3*pi);
y1 = sin(x);
y2 = sin(x - pi/4);
hold on;
p1 = plot(x,y1);
p3 = plot(x,y1, '*');
p2 = plot(x,y2);
p4 = plot(x,y2,'+');
hold off
%% Plot the first legend
lh = legend(ah, [p1 p2], 'p1', 'p2');
lh.Location='NorthEastOutside';
%% Copy the axes and plot the second legned
ah2 = copyobj( ah, gcf);
lh2 = legend(ah2, [p3 p4], 'p3', 'p4');
lh2.Location='SouthEastOutside';
  2 comentarios
Mark Gugliuzza
Mark Gugliuzza el 12 de Oct. de 2022
Love this approach and it works perfect. However, I am trying to do the same thing with a 2x2 tiled layout where each tile will end up needing multiple legends. However, it doesn't seem to work. This is the result:
I'm getting the axis to tile (1,1) with: ah = nexttile(T1); and everything else it the same
Any help would be appreciated

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by