The figure contains all the plot elements. The main way to create a figure in matplotlib
is to use pyplot
.
import matplotlib.pyplot as plt
fig = plt.figure()
You can optionally supply a number, which you can use to access a previously-created figure. If a number is not supplied, the last-created figure's ID will be incremented and used instead; figures are indexed starting from 1, not 0.
import matplotlib.pyplot as plt
fig = plt.figure()
fig == plt.figure(1) # True
Instead of a number, figures can also identified by a string. If using an interactive backend, this will also set the window title.
import matplotlib.pyplot as plt
fig = plt.figure('image')
To choose figure use
plt.figure(fig.number) # or
plt.figure(1)