In Python’s matplotlib.pyplot
the figsize
command allows you to determine the figure size, eg.
JavaScript
x
11
11
1
from numpy import linspace, sin
2
import matplotlib.pyplot as plt
3
4
x = linspace(-2,2,100)
5
y = sin(1.5*x)
6
7
plt.figure(figsize=(8,5))
8
plt.plot(x,y)
9
plt.savefig('myfigure.pdf')
10
plt.show()
11
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
.
Advertisement
Answer
Since you aim for saving/exporting your figure, you must pay attention to the right Figure Properties, namely:
PaperSize
, and
I tested the following code in Octave 5.1.0, but it should be fully MATLAB compatible:
JavaScript
1
6
1
x = linspace(-2, 2, 100);
2
y = sin(1.5 * x);
3
fig = figure('PaperUnits', 'inches', 'PaperSize', [8 5], 'PaperPosition', [0 0 8 5]);
4
plot(x, y);
5
saveas(fig, 'myfigure_octave.pdf', 'pdf');
6
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:
Hope that helps!