How do I set up a legend like this(two curves one legend then some gap)??

2 comentarios

VBBV
VBBV el 31 de En. de 2024
The plots you show have custom legends.
Try this link available in FEX where you can change the legend layout and behavior as you wanted
Why not plot an empty line along side and use a blank as its legend entry?
x = 0:0.01:10;
figure
plot(x, sin(x))
hold on
plot(x,cos(x));
plot(x,floor(x/5))
plot(x,NaN(size(x)))
xlim([-1 11])
str = ["sin" "cos" "custom" ""];
legend(str, 'NumColumns', 2)

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 30 de En. de 2024

0 votos

Set the original legend to 'Location','NE' and then use:
legend('boxoff')
Also, see Position Multiple Axes in Figure if you have not done so already.

6 comentarios

Shahriar Shafin
Shahriar Shafin el 31 de En. de 2024
@Star Strider I have no problem with the plots,in legend section I set the parameters before as u said but it didnt show like above .My figure mentioned below:
I am having real problems with MATLAB and this site this morning.
This renders correctly on my computer —
However it does not render correctly when I run the same code here —
F = openfig('fluence ttm non-equib new.fig');
F.Visible = 'on'; % Make Figure Visible
% get(F)
lgd = F.Children(2); % Get Legend Handle
pos = lgd.Position; % Get Legend Position
lgd.Position = pos+[-0.04 -0.20 0.06 0.13]; % Adjust Legend Position [Left Lower Width Height]
% daspect([1 2 1]) % Use This To Stretch The Axes If Neceessary
The approach is to adjust the legend position until it works.
Is this the result you want, or something else?
.
Shahriar Shafin
Shahriar Shafin el 31 de En. de 2024
@Star Strider thanks a lot for ur concern but I wanted to do like the upper figure's legend mentioned in my question(dotted plot and line plot under the same legend),want to make it 5 legends(10,20,30,40,50) rather than 10 (10,10,20,20,30,30,40,40,50,50).
That was not made clear.
Try this —
F = openfig('fluence ttm non-equib new.fig');;
F.Visible = 'on'; % Make Figure Visible
% get(F)
lgd = F.Children(2); % Get Legend Handle
lgd.String = lgd.String(1:2:end); % Use Every Other Element In The String
lgd.String = strrep(lgd.String,'J',' J'); % Add A Space (Optional)
lgd.String = strrep(lgd.String,'m2','m^2'); % Add Superscripts (Optional)
pos = lgd.Position; % Get Legend Position
lgd.Position = pos+[-0.04 -0.10 0.06 -0.01]; % Adjust Legend Position By Adding ± Offsets [Left Lower Width Height]
% daspect([1 2 1]) % Use This To Stretch The Axes If Neceessary
Again, it does not render well here, however it does on my computer and in MATLAB Online.
.
Shahriar Shafin
Shahriar Shafin el 31 de En. de 2024
@Star Strider got it ,thanks a lot.
Star Strider
Star Strider el 31 de En. de 2024
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 30 de En. de 2024

0 votos

I think you'd have to build it up manually with the plot and text functions.
VINAYAK LUHA
VINAYAK LUHA el 30 de En. de 2024
Hello Shahriar,
It looks like you want to overlay two sets of axes, with the larger one encompassing the smaller, and include a common legend for both in MATLAB. To accomplish this task, adhere to the following instructions:
  • First, Create an axes called "larger" and add plots to it.
  • Next, define a another axes called "smaller" to be positioned within the "larger" axes.
  • Finally,to create a common legend, plot invisible lines and assign the legends to them.
Here's the MATLAB code for your reference:
x = linspace(1, 10, 100);
y1 = log(x);
y2 = log(x.^2);
y3 = log(x.^3);
y4 = log(x.^4);
figure;
larger = axes;
plot(larger, x, y1, 'r-', x, y2, 'b-', x, y3, 'g-', x, y4, 'k-');
hold(larger, 'on');
smaller = axes('Position', [0.2 0.6 0.25 0.25]);
plot(smaller, x, y1, 'r-', x, y2, 'b-', x, y3, 'g-', x, y4, 'k-');
h1 = plot(larger, NaN, NaN, 'r-');
h2 = plot(larger, NaN, NaN, 'b-');
h3 = plot(larger, NaN, NaN, 'g-');
h4 = plot(larger, NaN, NaN, 'k-');
legend(larger, [h1, h2, h3, h4], {'log(x)', 'log(x^2)', 'log(x^3)', 'log(x^4)'});
Hope this helps you to understand how to have a common legend for two nested axes
Regards
Vinayak Luha

Etiquetas

Preguntada:

el 30 de En. de 2024

Comentada:

el 31 de En. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by