How to use matplotlib tight layout with Figure? How to use matplotlib tight layout with Figure? python python

How to use matplotlib tight layout with Figure?


Just call fig.tight_layout() as you normally would. (pyplot is just a convenience wrapper. In most cases, you only use it to quickly generate figure and axes objects and then call their methods directly.)

There shouldn't be a difference between the QtAgg backend and the default backend (or if there is, it's a bug).

E.g.

import matplotlib.pyplot as plt#-- In your case, you'd do something more like:# from matplotlib.figure import Figure# fig = Figure()#-- ...but we want to use it interactive for a quick example, so #--    we'll do it this wayfig, axes = plt.subplots(nrows=4, ncols=4)for i, ax in enumerate(axes.flat, start=1):    ax.set_title('Test Axes {}'.format(i))    ax.set_xlabel('X axis')    ax.set_ylabel('Y axis')plt.show()

Before Tight Layout

enter image description here

After Tight Layout

import matplotlib.pyplot as pltfig, axes = plt.subplots(nrows=4, ncols=4)for i, ax in enumerate(axes.flat, start=1):    ax.set_title('Test Axes {}'.format(i))    ax.set_xlabel('X axis')    ax.set_ylabel('Y axis')fig.tight_layout()plt.show()

enter image description here