问题
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:
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.
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);
- The
legendflex
file looks very interesting, but I think the control over the whitespace buffering was only for the position of the legend itself.
回答1:
You can add any of the first 32 ascii codes (non printable characters) to create a space. Not sure it will work with psfrag though.
Here, the piece of code creates 30 spaces using ASCII code 3.
h_plot = plot([0 1], [0 1]);
h_legend = legend([ 'A' repmat(char(3),1,30) ],0);
EDIT
Another possibility. You can use the handles from legend. Here, changing the legend's text from 10 to 1 characters do not modify the size of the legend box.
[~,OBJH,~,~] = legend('0123456789'); % display a legend of 10 characters
set(OBJH(1), 'String', 'A'); % change its String to 1 character
-- see comments: saving as .eps
brings back the old string in the image file created.
回答2:
To add extra withspace to legend, simply use
{ } -> { "add how much space you wish between two brackets" }
for example:
legend( 'A{ }','b{ }' )
回答3:
The function legend
has an argument 'Location'. You can pass a vector to it, i.e.
plot(1:10)
legend('Sometext', 'Location', [0.20, 0.1, 0.75, 0.25])
where the latter vector can be interpreted as [Position_Right_in_pct, Position_Top_in_pct, Horizontal_Stretch, Vertical_Stretch]
来源:https://stackoverflow.com/questions/25495768/add-extra-whitespace-to-matlab-legend-for-use-with-psfrag