Setting the figure size independent of the legend size

52 views (last 30 days)
Hi,
I want to create a figure with an outside legend (SouthOutside) where the actual figure size, so the length of the x-axis and y-axis is set to a specific value. The idea behind is that I have multiple figures in a report with varying legend sizes, but the actual figure should always be the same.
I tried to use this, to get a figure of the size 10.8 by 8.1cm.
set(gcf,'units','centimeters','Position',[10.0,10.0,10.8,8.1]);
However, this determines the size of the window of the figure, so if the legend gets bigger the figure gets smaller.
Does anyone have an idea how to get a fixed figure size? Help would be highly appreciated.
I am pretty new to MATLAB so I don´t know if that makes it easier but I only need the figure as a .pdf so it doesn´t need to be shown in a window. Currently I use
exportgraphics
to export the figure as a .pdf.
(I use MATLAB R2020a)

Accepted Answer

Adam Danz
Adam Danz on 6 Oct 2020
Edited: Adam Danz on 14 Jan 2021
It sounds like you want to expand the axis size which would also require adjusting the figure size.
Here's a demo.
fig = figure;
plot(magic(5))
lh = legend('Location','SouthOutside');
% Define desires axis size
newAxSize = [10.8, 8.1]; %[width, height] cm
% Adjust axis and figure sizes
ax = gca(fig);
ax.Units = 'Centimeters';
sizeDiff = newAxSize - ax.Position(3:4);
fig.Units = 'centimeters';
fig.Position(3:4) = fig.Position(3:4) + sizeDiff;
ax.Position(3:4) = [10.8, 8.1];
movegui(fig) % make sure figure is not off-screen

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!