Obtain short lines in a legend in a subplot

1 view (last 30 days)
I am using the code below to generate the plot below. I want to shorten the blue and orange marker lines in the legend. I cannot get the 'Children' of the legend, since this is a '0×0 empty GraphicsPlaceholder array'. I couldn't find a solution that works in my code. Thanks in advance 😊
%%Clear and import properties
clear
close all
%% Plot size
f=figure(1);
width = 18;
height = 9;
set(gcf, 'Units', 'centimeters')
set(gcf, 'Position', [-30 8 width height])
set(gcf, 'PaperPositionMode', 'auto')
%Relative positions
%Horizontal
a_h=0.05;
c_h=0.05;
e_h=c_h;
g_h=0.01;
rest=(1-a_h-c_h-e_h-g_h);
b_h=rest/3;
d_h=rest/3;
f_h=rest/3;
%Vertical
a_v=0.1;
c_v=0.13;
e_v=0.02;
rest=(1-a_v-c_v-e_v);
b_v=0.4*rest;
d_v=0.6*rest;
%% Actual plot
%ax(2)=subplot(3,2,2);
%ax(3)=subplot(3,2,3);
ax(1)=subplot(2,3,[1 3]);
pos=get(ax(1),'Position'); %hpos vpos width height
pos(1)=a_h;%horizontal position
pos(2)=a_v+b_v+c_v;%vertical position
pos(3)=1-a_h-g_h;%width
pos(4)=d_v;%height
set(ax(1), 'Position', pos);
ax(2)=subplot(2,3,4);
pos=get(ax(2),'Position'); %hpos vpos width height
pos(1)=a_h;%horizontal position
pos(2)=a_v;%vertical position
pos(3)=b_h;%width
pos(4)=b_v;%height
set(ax(2), 'Position', pos);
ax(3)=subplot(2,3,5);
pos=get(ax(2),'Position'); %hpos vpos width height
pos(1)=a_h+b_h+c_h;%horizontal position
pos(2)=a_v;%vertical position
pos(3)=b_h;%width
pos(4)=b_v;%height
set(ax(3), 'Position', pos);
ax(4)=subplot(2,3,6);
pos=get(ax(2),'Position'); %hpos vpos width height
pos(1)=a_h+b_h+c_h+d_h+e_h;%horizontal position
pos(2)=a_v;%vertical position
pos(3)=b_h;%width
pos(4)=b_v;%height
set(ax(4), 'Position', pos);
axes(ax(1))
plot(linspace(-10,10,100),-linspace(-10,10,100).^2)
axes(ax(2))
plot(linspace(-10,10,100),-linspace(-10,10,100).^2)
hold on
plot(linspace(-10,10,100),+linspace(-10,10,100).^2)
legh=legend(["1^{st} Measurement", "2^{nd} Measurement"]);
set(legh,'Position',[0.235 0.44 0.01 0.01],'Orientation','horizontal')
axes(ax(3))
plot(linspace(-3,3,100),-linspace(-3,3,100).^3)
hold on
plot(linspace(-3,3,100),+linspace(-3,3,100).^3)
axes(ax(4))
plot(linspace(-10,10,100),-linspace(-10,10,100))
hold on
plot(linspace(-10,10,100),+linspace(-10,10,100))
  1 Comment
dpb
dpb on 14 Dec 2021
Don't think you'll be able to do anything to the line drawn by legend -- TMW has pretty-well locked down the properties inside it so you can't do much munging on them anymore. The underlying axes of the legend object is now opaque to the point of being almost black; you can't get access to the line handles that make up the displayed lines.
I think it's accept the line style associated with the line handle or you'll have to create a custom pseudo-legend object of your own.

Sign in to comment.

Accepted Answer

Alagu Sankar Esakkiappan
Alagu Sankar Esakkiappan on 16 Dec 2021
Hi Moritz,
You may change the length of line inside the legend section of a plot using one of its hidden properties "ItemTokenSize". From your example, legh.ItemTokenSize is a 2x1 array where the 1st element represents the length of legend line. By default, it is initialized to [ 30 18 ]. You may change your line length to a shorter line by modifying legh.ItemTokenSize(1) to a smaller value.
You may refer to the following example for modifiying legend's line length:
%%Clear and import properties
clear
close all
%% Plot size
f=figure(1);
width = 18;
height = 9;
set(gcf, 'Units', 'centimeters')
set(gcf, 'Position', [-30 8 width height])
set(gcf, 'PaperPositionMode', 'auto')
%Relative positions
%Horizontal
a_h=0.05;
c_h=0.05;
e_h=c_h;
g_h=0.01;
rest=(1-a_h-c_h-e_h-g_h);
b_h=rest/3;
d_h=rest/3;
f_h=rest/3;
%Vertical
a_v=0.1;
c_v=0.13;
e_v=0.02;
rest=(1-a_v-c_v-e_v);
b_v=0.4*rest;
d_v=0.6*rest;
%% Actual plot
%ax(2)=subplot(3,2,2);
%ax(3)=subplot(3,2,3);
ax(1)=subplot(2,3,[1 3]);
pos=get(ax(1),'Position'); %hpos vpos width height
pos(1)=a_h;%horizontal position
pos(2)=a_v+b_v+c_v;%vertical position
pos(3)=1-a_h-g_h;%width
pos(4)=d_v;%height
set(ax(1), 'Position', pos);
ax(2)=subplot(2,3,4);
pos=get(ax(2),'Position'); %hpos vpos width height
pos(1)=a_h;%horizontal position
pos(2)=a_v;%vertical position
pos(3)=b_h;%width
pos(4)=b_v;%height
set(ax(2), 'Position', pos);
ax(3)=subplot(2,3,5);
pos=get(ax(2),'Position'); %hpos vpos width height
pos(1)=a_h+b_h+c_h;%horizontal position
pos(2)=a_v;%vertical position
pos(3)=b_h;%width
pos(4)=b_v;%height
set(ax(3), 'Position', pos);
ax(4)=subplot(2,3,6);
pos=get(ax(2),'Position'); %hpos vpos width height
pos(1)=a_h+b_h+c_h+d_h+e_h;%horizontal position
pos(2)=a_v;%vertical position
pos(3)=b_h;%width
pos(4)=b_v;%height
set(ax(4), 'Position', pos);
axes(ax(1))
plot(linspace(-10,10,100),-linspace(-10,10,100).^2)
axes(ax(2))
plot(linspace(-10,10,100),-linspace(-10,10,100).^2)
hold on
plot(linspace(-10,10,100),+linspace(-10,10,100).^2)
legh=legend(["1^{st} Measurement", "2^{nd} Measurement"]);
%==============Legend's Line Length modified here===================%
legh.ItemTokenSize(1) = 15;
%===================================================================%
set(legh,'Position',[0.235 0.44 0.01 0.01],'Orientation','horizontal')
axes(ax(3))
plot(linspace(-3,3,100),-linspace(-3,3,100).^3)
hold on
plot(linspace(-3,3,100),+linspace(-3,3,100).^3)
axes(ax(4))
plot(linspace(-10,10,100),-linspace(-10,10,100))
hold on
plot(linspace(-10,10,100),+linspace(-10,10,100))

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by