Size plot and legend position programmatically for publication

23 views (last 30 days)
Preparing a plot for a publication is far from trivial as I've learned over the past couple of days, so I was hoping for some help. There are three things I would like help with:
  1. Size plot acccording to journal requirements
  2. Programmatically position the legend outside of the plot (in the 'southoutside' position, centre aligned) without the plot obscuring the legend. Note if I use 'southoutside', the legend does not centre align, hence why I have not used it below
  3. Print image in accordance with sizing requirements
For reference, I am seeking to size the figure 84 mm x 84 mm to satisfy the requirements for Springer Guidelines for Illustrations
Here is a much simplified version of some code to demonstrate my issue:
% Inputs
x_pos = [2.5,5,7.5];
depth_0 = [0,0,0];
depth_nom = [-0.0038 -0.0035 -0.004];
set(0,'DefaultFigureWindowStyle','normal')
set(0,'defaulttextinterpreter','latex')
set(0,'DefaultLegendInterpreter','latex')
set(0,'defaultAxesTickLabelInterpreter','latex');
h(1) = figure;
% Plot
plot(x_pos,depth_0,'marker','o','MarkerFaceColor','r','MarkerEdgeColor','r','MarkerSize',1)
hold on
plot(x_pos,depth_nom,'marker','o','MarkerSize',1)
% Figure properties
h(1).Units = 'centimeters';
h(1).Position(3) = 8.4;
h(1).Position(4) = h(1).Position(4); % aspect ratio 1:1
FontSize = 12;
set(h(1).Children, 'FontSize', FontSize);
axis square
axis tight
xlim([0 10])
xticks([0:2.5:10])
ylim([-0.025 0.025])
yticks(-0.025:.025:.025)
ylabel('$\eta$ [m]')
xlabel('$x$ [m]')
legendWidth = .3; legendHeight = .3; legendX0 = .5-(legendWidth/2); legendY0 = .0001; % legend sizing (normalised)
set(gca,'LooseInset',max(get(gca,'TightInset'), 0.02)) % remove unnecessary white space
% Get the object handles
haxis = get(h(1),'CurrentAxes');
set(haxis, 'Units', 'centimeters')
% Set the figure size and position
pos = get(h(1), 'Position');
outerpos = get(haxis, 'OuterPosition');
set(haxis, 'OuterPosition',[0, 0, outerpos(3), outerpos(4)]);
set(h(1), 'Position', [pos(1), pos(2), outerpos(3), outerpos(4)]);
% Paper position in the eps
set(h(1), 'PaperPositionMode', 'auto');
% Legend
leg = legend('Zero flow','$U_{0}$','FontSize',FontSize,'NumColumns',2,'AutoUpdate','off');
legend boxoff
set(leg, 'Position', [legendX0, legendY0, legendWidth,legendHeight])
exportgraphics(figure(1), 'test.pdf', 'ContentType', 'vector');
My preference is to do this programmatically rather than manually sizing and positioning the figure properties.
When I insert the printed image into my Latex document, it is categorically not 84 mm wide to fit a single column.
I have tried a few of the functions on File Exchange ('Export Fig' - doesn't print pdfs in 2021b; 'Pro Plot' - doesn't deal with legends outside of the plot; fig- the exact figure size and no white space' - didn't resolve my issues)
I'd be very grateful for any help. Thank you.
  2 Comments
dpb
dpb on 19 Jun 2023
Edited: dpb on 19 Jun 2023
You're writing the legend on top of the xlabel position for starters by not letting the axes resize to make room for it.
If I just use
hLg=legend('Zero flow','$U_{0}$','FontSize',FontSize,'NumColumns',2,'Location','southoutside');
it looks pretty good to me and centered on the axes of the figure. Personally, I think that would look better than trying to center it on the whole figure.
I know there have been issues with printing to size for since forever; I've never had the need, thankfully, my publication days were too far in the past to submit electronically.
Daniel Rowe
Daniel Rowe on 19 Jun 2023
Thanks for your response. To be clear, the issue was slightly conflated if the screen was docked and I manually adjusted the legend position, which led to the plot auto resizing. Anyway, since then, I discovered that by changing the 'DataAspectRatioMode' attribute to 'manual', the auto-resizing is switched off, furthermore, I gave up with the legend outside of the plot and reverted to 'Location','best' as this seems more robust.

Sign in to comment.

Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!