Setting the figure size independent of the legend size

61 visualizaciones (últimos 30 días)
Markus Schaffer
Markus Schaffer el 6 de Oct. de 2020
Editada: Adam Danz el 14 de En. de 2021
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)

Respuesta aceptada

Adam Danz
Adam Danz el 6 de Oct. de 2020
Editada: Adam Danz el 14 de En. de 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
  2 comentarios
Markus Schaffer
Markus Schaffer el 6 de Oct. de 2020
Thank you very much for this fantastic fast and helpful answer!
Adam Danz
Adam Danz el 6 de Oct. de 2020
Glad I could help out.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by