Printing figure as pdf with long legend produces unncecessary whitespace

10 ビュー (過去 30 日間)
I am creating pdfs from figures with automated scripts for scientific purposes. Recently I had the need to use rather long legend entries to describe my data. When the figures are created, the plots look fine, upon printing to pdf, however, unnecessary whitespace is inserted on the right side of the legend block if the legend entries are long, which messes up my figure. It's frustrating because the figure looks fine when it is made but I wasn't able to get the exact same pdf-representation. Here's a minimal code example:
plot([0 1], [0 1]);
hold on;
plot([0 1], [1 0]);
legend('+', '-');
print(gcf, 'short.pdf', '-dpdf');
legend('A linear function with positive slope', 'A linear function with negative slope');
print(gcf, 'long.pdf', '-dpdf');
Thanks in advance.

採用された回答

MyNickname
MyNickname 2024 年 2 月 21 日
編集済み: MyNickname 2024 年 2 月 21 日
Thanks for your detailed response. Changing the legend position does the trick for the figure but upon printing to pdf, it still moves the whole content of the box (whether the actual box is drawn are not) to the left.
BTW, I was able to reproduce the problem with Version R2023b and Octave on another machine running Ubuntu 22 (the other computer uses Ubuntu 20). In addition, I used the online Matlab-Version provided by MathWorks so I can rule out any system-related bugs based on other packages.
Your suggestion with the fontname was helpful though, as it does not happens for monospaced fonts like Courier. So chosing one of these is a workaround for those who encounter the same problem as I did, albeit not an elegent one IMO. I also found that the blank space can be forced to appear at a different place within the legend by using braces, e.g.
leg = legend( 'Linear {}function {}with {}positive {}slope', 'Linear {}function {}with {}negative {}slope' );
produces the following pdf, which is an improvement but maybe not a viable option depending on the text in the legend. The braces take effect only in the pdf not the figure, though!
Anyway I'm sort of satisified with my plots now. It's just frustrating that the figures I want look exactly as they should and the print to pdf command messes it up unnecessarily.
  1 件のコメント
Constantino Carlos Reyes-Aldasoro
Constantino Carlos Reyes-Aldasoro 2024 年 2 月 21 日
As you say, this is a workaround rather than a proper solution, but if it works, it works. I am using R2023a in a windows machine, so probably the issue is to do with how PDFs are generated in unix and online (not sure which operating system that would be).
One final tip, spaces can be forced if you use the ascii code with strcat:
mystring = strcat('A linear function',32,32,32,32,32,32,'with positive',32,'slope')
mystring = 'A linear function with positive slope'
That is sometimes useful for titles when you want a space after = or : and it not always appears.
If this settles the issue, could you please accept the answer?

サインインしてコメントする。

その他の回答 (1 件)

Constantino Carlos Reyes-Aldasoro
Constantino Carlos Reyes-Aldasoro 2024 年 2 月 20 日
I am not sure that I understand the problem, I tried in my computer and obtained a figure like the one below (this is jpeg, but the PDF looks the same). Can you upload your figure to show where the unnecessary space is?
  2 件のコメント
MyNickname
MyNickname 2024 年 2 月 20 日
Your figure looks like as mine but once printed to pdf it looks as follows (took a screenshot of the pdf, so it would show here). The whitespace is on the right side of the box. I'm using MATLAB version R2021a BTW.
Constantino Carlos Reyes-Aldasoro
Constantino Carlos Reyes-Aldasoro 2024 年 2 月 20 日
How strange, mine looked exactly the same without that space. OK, what you can do is to grab the handle of the legend and then manipulate the dimensions manually to make it exactly the size you want. So, first grab the handle like this:
plot([0 1], [0 1]);
hold on;
plot([0 1], [1 0]);
legend('+', '-');
print(gcf, 'short.pdf', '-dpdf');
hLegend = legend('A linear function with positive slope', 'A linear function with negative slope');
You can see the properties of the handle:
hLegend
hLegend =
Legend (A linear function with positive slope, A linear function with negative slope) with properties: String: {'A linear function with positive slope' 'A linear function with negative slope'} Location: 'northeast' Orientation: 'vertical' FontSize: 9 Position: [0.4016 0.8210 0.4840 0.0797] Units: 'normalized' Use GET to show all properties
You can now manipulate like this:
hLegend.FontName='Courier';
hLegend.Position = [0.3 0.82 0.47 0.08];
You could also remove the color of the background and edge and then you would not notice the extra space
hLegend.Color='none';
hLegend.EdgeColor='none';
Hope this helps to sort out your problem!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by