Search code examples
pythonmatlabmatplotlibgraphmatlab-figure

Matlab equivalent to Python's figsize


In Python's matplotlib.pyplot the figsize command allows you to determine the figure size, eg.

from numpy import linspace, sin
import matplotlib.pyplot as plt

x = linspace(-2,2,100)
y = sin(1.5*x)

plt.figure(figsize=(8,5))
plt.plot(x,y)
plt.savefig('myfigure.pdf')
plt.show()

Is there an equivalent command in Matlab that does this? These old posts show different solutions but none are as clean as Python's figsize.


Solution

  • Since you aim for saving/exporting your figure, you must pay attention to the right Figure Properties, namely:

    I tested the following code in Octave 5.1.0, but it should be fully MATLAB compatible:

    x = linspace(-2, 2, 100);
    y = sin(1.5 * x);
    fig = figure('PaperUnits', 'inches', 'PaperSize', [8 5], 'PaperPosition', [0 0 8 5]);
    plot(x, y);
    saveas(fig, 'myfigure_octave.pdf', 'pdf');
    

    I created a myfigure_python.pdf using your code. Both exported figures have a size of 203.2 x 127,0 mm which is 8 x 5 inches, and look quite similar, see the following screenshot. myfigure_python.pdf is on the left, myfigure_octave.pdf on the right side:

    Output

    Hope that helps!