3
votes

I am using the matlab to plot some project figures, see the blow figure. Now I am trying to cut the legend width so that the line won't look so wide. I tried these command as suggest by Benoit_11:

[~,icons,~,~] = legend(leg,'location','northwest');
hline = icons(2);
linedata = get(hline,'xdata');
newdata = [linedata(1)+0.2 linedata(2)];
set(hline,'xdata',newdata,'linewidth',1)

I am using the for loop to plot these figures because I have multiple figures to analysis at the same time. Now I can change the length of the legend line right now. But I got another problem: if I have different length of legend text, even if I set the same starting point and end point, I will get different length for the line in the end (you can see that from the figures). I tried to modify icon(1) but always got the error. Any suggestions? enter image description here

2

2 Answers

3
votes

There are 2 things you are not doing right with your code (aside the fact that you use size as the handles to the legend...that's risky because size is a built-in function):

1) Calling legend with only 1 argument returns a handle to the legend object and getting its position actually gives you the position of the box enclosing the legend, i.e. the text + the line.

2) Using this line:

p(3) = p(3) - 0.06;

does modify the position, however you would need to set the new position of the legend with something like the following for the changes to be effective:

set(HandleToLegend,'Position',p)

To come back to your question, the trick is to assign many outputs during the call to legend; you can then modify specific elements of the legend object.

Actually we only need 1 of the 4 output arguments, called icons in the docs so I'll stick with the notation. Then, we can get the XData property of the line and modify it as we want. The XData is actually a 2-element vector:

[StartingPoint EndingPoint]

so changing one or the other (or both) will change the length of the line displayed in the legend box.

Here is the whole code with comments; I changed the length and linewidth of the line in the 2nd plot to highlight the changes.

clear
clc
close all
x = 1:10;
y = rand(1,10);
figure;

%// Default case
subplot(1,2,1)
plot(x,y);

legend('First plot','Location','NorthWest');

title('Before','FontSize',18);

%// With modifications
subplot(1,2,2)
plot(x,y);
title('After','FontSize',18);

%//========================
%// Change the legend here
%//========================

%// The "icons" output is what you want
[~,icons,~,~] = legend('First plot','Location','NorthWest');

%// icons(1) is the text of the current element in the legend Here its 'First plot'
i_1 = get(icons(1)); %// access the properties with this command.

%// icons(2) is the line associated with that text. Here the blue line.
i_2 = get(icons(2));

%// Mhh I don't know what icons(3) represents haha sorry about that.
i_3 = get(icons(3));


%// Get the actual line
hline = icons(2);

%// Fetch its XData property
LineData = get(hline,'XData')

%// Play with those 2 elements to see the output change.
NewData = [LineData(1)+.2 LineData(2)-.01];

%// Apply the changes
set(hline,'XData',NewData,'LineWidth',3)

Which gives the following:

enter image description here

-1
votes

You need to set the value of the Position property, you can just change the vector p. p does not affect the plot at all, it's just a vector of numbers. You have to modify it, then apply it back to the plot, using

set(size,'Position',p)

There does seem to be a minimum width of the legend however.