Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add extra whitespace to matlab legend (for use with psfrag)

Tags:

matlab

figures

How to pad white-space on a matlab legend to the right of the text? I am using a combination of psfrag (and adobe illustrator for a few other diagram modifications), and will replace the placeholder text in a figure with an equation. The problem is that it tightly bounds the box on the placeholder text, while I want to leave room for my equation

Start with the simple figure;

h_plot = plot([0 1], [0 1]); 
h_legend = legend('A',0);

The spacing I really want would be something like this with

h_plot = plot([0 1], [0 1]); 
h_legend = legend('A!!!!!!!!',0);

where the !!!!!!!! is actually whitespace, and it really is stored as one character 'A'.

A few things which did not seem to work:

  1. One obvious solution is: just add in text such as "A!!!!!!!!!!!!" and replace the whole text with my equation in psfrag. However, if I touch the file with Adobe Illustrator, then it converts the text to individual characters, which breaks psfrag (see http://engineeringrevision.com/314/getting-illustrator-to-play-nicely-with-psfrag/ for example). So I really need to just have the 'A' character as the string.

  2. Another is to try to stretch the box, but chanking the position or the aspect ratio streches the text and line accordingly.

For example, the following just stretches the width

h_plot = plot([0 1], [0 1]); 
h_legend = legend('A',0);
leg_pos = get(h_legend,'position'); leg_pos(3) = leg_pos(3) * 2;
set(h_legend, 'position', leg_pos);
  1. The legendflex file looks very interesting, but I think the control over the whitespace buffering was only for the position of the legend itself.
like image 206
jlperla Avatar asked Aug 25 '14 23:08

jlperla


People also ask

How do you change the size of a legend in MATLAB?

You can change the font size for a MATLAB legend by setting the 'FontSize' property of the Legend object. For example, plot four lines. Create a legend and assign the Legend object to the variable 'lgd'. Then, use dot notation to access the 'FontSize' property and set the value to 14 points.

How do I manually add a legend in MATLAB?

x = linspace(0,10); y1 = sin(x); y2 = sin(0.9*x); y3 = sin(0.8*x); y4 = sin(0.7*x); y5 = sin(0.6*x); y6 = sin(0.5*x); plot(x,y1,'DisplayName','sin(x)') hold on plot(x,y2,'DisplayName','sin(0.9x)') plot(x,y3,'DisplayName','sin(0.8x)') plot(x,y4,'DisplayName','sin(0.7x)') plot(x,y5,'DisplayName','sin(0.6x)') plot(x,y6,' ...

How do you skip items in legend in MATLAB?

1- Select the curve you don't want have legend. 2- Go to the "more properties" (while the curve is still selected). 3- Turn "HandleVisibility" off.

How do I change the legend box position in MATLAB?

To move the legend to a different tile, set the Layout property of the legend. Determined by Position property. Use the Position property to specify a custom location.


1 Answers

To add extra withspace to legend, simply use

{          }  -> { "add how much space you wish between two brackets" }

for example:

legend( 'A{     }','b{    }' ) 
like image 198
user6514940 Avatar answered Nov 15 '22 08:11

user6514940