import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
plt.show()
According to offical Matplotlib document(https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.figure) A figure function will
"If num is provided, and a figure with this id already exists, make it active, and returns a reference to it. "
I tried do the above on my Ipython without plt.figure, but it showed the two required pictures still.
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, etc.
It is necessary to explicitly use plt. figure() when we want to tweak the size of the figure and when we want to add multiple Axes objects in a single figure.
figure. The figure module provides the top-level Artist , the Figure , which contains all the plot elements. The following classes are defined SubplotParams control the default spacing of the subplots Figure. Top level container for all plot elements.
figsize is a tuple of the width and height of the figure in inches, and dpi is the dots-per-inch (pixel per inch).
There are three cases where plt.figure
is useful:
Obtaining a handle to a figure. In many cases it is useful to have a handle to a figure, i.e. a variable to store the matplotlib.figure.Figure
instance in, such that it can be used later on. Example:
fig = plt.figure()
#... other code
fig.autofmt_xdate()
Set figure parameters. An option to set some of the parameters for the figure is to supply them as arguments to plt.figure
, e.g.
plt.figure(figsize=(10,7), dpi=144)
Create several figures. In order to create several figures in the same script, plt.figure
can be used. Example:
plt.figure() # create a figure
plt.plot([1,2,3])
plt.figure() # create another figure
plt.plot([4,5,6]) # successive commands are plotted to the new figure
In many other cases, there would not actually be any need to use plt.figure
. Using the pyplot interface, a call to any plotting command is sufficient to create a figure and you can always obtain a handle to the current figure with plt.gcf()
.
From another perspective it is often desired not only to have a handle to the figure but also to an axes to plot to. In such cases, the use of plt.subplots
is more favorable, fig, ax = plt.subplots()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With