Size of the legend versus paper size

3 views (last 30 days)
The following code attempts to produce three PDF of the same figure, with three different paper sizes. I would like, and it should be possible, to get more or less the same PDF except that it is scaled to the desired size. I mostly achieve this goal, except for the legend, whose rendering differs depending on the paper size in a way that seems uncontrolable to me. Especially, I did not achieve to use leg.Position to make the legend look the same in all three pdf files. Am I missing something? Or should it be considered as a bug?
x = linspace(0, pi, 100);
fig = figure;
curve = plot(x,sin(x));
ax = fig.CurrentAxes;
fig.Units = 'pixels';
fig.Position = [1 1 560 420];
ax.Units = 'pixels';
ax.Position = [28 21 504 378];
leg = legend('sin(x)');
for paperwidth = [8 12 50]
% Scale the font size and line widths with the paperwidth
ax.FontSize = 0.7 * paperwidth;
ax.LineWidth = 0.05 * paperwidth;
curve.LineWidth = 0.15 * paperwidth;
leg.FontSize = 0.7 * paperwidth;
% Sets the paper size to match the geometry of the figure
% with a a given width.
fig.PaperUnits = 'centimeters';
paperheight = paperwidth * fig.Position(4)/fig.Position(3);
fig.PaperSize = [paperwidth paperheight];
fig.PaperUnits = 'normalized';
fig.PaperPosition = [0 0 1 1];
% Generates the pdf
drawnow;
print(fig, ['foo' num2str(paperwidth) '.pdf'], '-dpdf');
end

Accepted Answer

Raunak Gupta
Raunak Gupta on 29 Jan 2020
Hi,
I tried with the code provided in the question and I saw that Legend FontSize is properly getting updated. You may see the attached pdfs with 100% size (Not the default fit page version while viewing in a pdf reader). So, the legend’s text is getting updated in terms of pagewidth. Since the colorbar icon near the Legend text is not dependent on the pagewidth and cannot be modified, that might look different when looking the pdf and out of sort. Currently changing the size of colorbar icon manually is not supported but the Legend FontSize is properly getting updated as mentioned here.
Hope this clear the query.
  2 Comments
Sylvain Chevillard
Sylvain Chevillard on 30 Jan 2020
Hi.
First of all, thank you for your answer.
I agree that the FontSize is getting updated properly. I never denied it. I said that the rendering of the legend was different (this includes of course the size and width of the colorbar icon, and the shape of the box around the legend).
The point of my question was "in a way that seems uncontrolable". For the font size, it is obviously controlable. Your answer brings me an information I did not notice: the colorbar icon has indeed the same size regardless of the paper size (roughly 1cm). And you confirm me that this parameter in uncontrolable.
Setting the size of the colorbar the same value regardless of any other parameters is really strange to me. The Position attribute of the legend theoretically allows one to choose the position and size of the legend. But it really affects only the box around, and its value is not even honored when it is too small. It would be much more logic that the colorbar icon (which is automatically drawn inside the legend object) would have its size fixed by the size of the legend object, as it is set by the Position attribute. Othewise, the purpose of this attribute is mostly discarded.
All the point of being able to finely tune a figure programmatically, is to allow the customer to automatize a nice rendering of their figure. I would like to be able to have my figure look the same when I render it for an article and when I render it for a poster. Right now, I cannot, which means that: either I re-program a customed legend feature (which is very painful and a pitty because this feature already exists) or I need to have recourse to a third-party tool to scale properly the figure which is nice for a given paper size to another paper size.
Raunak Gupta
Raunak Gupta on 30 Jan 2020
Hi,
I have brought this issue to the notice of our developers. They will investigate the matter further. Also I have attached the Use Case that is mentioned in the last comment.

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!