matplotlib で figureの大きさを変える方法

import math
import numpy as np
from matplotlib import pyplot
fig = pyplot.figure(figsize=(12, 4))
pi = math.pi   #mathモジュールのπを利用
 
x = np.linspace(0, 2*pi, 100)  #0から2πまでの範囲を100分割したnumpy配列
y = np.sin(x)
# adjustFigAspect(fig,aspect=.5)
ax = fig.add_subplot(111)
ax.plot(range(10),range(10))
def adjustFigAspect(fig,aspect=1):
    '''
    Adjust the subplot parameters so that the figure has the correct
    aspect ratio.
    '''
    xsize,ysize = fig.get_size_inches()
    minsize = min(xsize,ysize)
    xlim = .4*minsize/xsize
    ylim = .4*minsize/ysize
    if aspect < 1:
        xlim *= aspect
    else:
        ylim /= aspect
    fig.subplots_adjust(left=.5-xlim,
                        right=.5+xlim,
                        bottom=.5-ylim,
                        top=.5+ylim)

stackoverflow.com

stackoverflow.com

こんな感じにデフォルト設定を変えるのもあり

defaulte_fig_size = plt.rcParams["figure.figsize"]
plt.rcParams["figure.figsize"] = [6.0, 18.0]
...
plt.tight_layout()
plt.show()
plt.rcParams["figure.figsize"] = defaulte_fig_size

codeyarns.com