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()
“If num is provided, and a figure with this id already exists, make it
active, and returns a reference to it. “
我尝试在没有plt.figure的情况下在我的Ipython上执行上述操作,但它仍然显示了两张所需的图片.
解决方法:
plt.figure有三种情况有用:
>获取图形的句柄.在许多情况下,拥有图形的句柄是有用的,即用于存储matplotlib.figure.Figure实例的变量,以便稍后可以使用它.例:
fig = plt.figure()
#... other code
fig.autofmt_xdate()
>设置图形参数.为图设置一些参数的选项是将它们作为参数提供给plt.figure,例如,
plt.figure(figsize=(10,7), dpi=144)
>创建几个数字.为了在同一个脚本中创建多个图形,可以使用plt.figure.例:
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
在许多其他情况下,实际上不需要使用plt.figure.使用pyplot接口,调用任何绘图命令就足以创建一个图形,并且您始终可以使用plt.gcf()获取当前图形的句柄.
从另一个角度来看,通常不仅需要具有图形的手柄而且还需要具有要绘制的轴.在这种情况下,使用plt.subplots更有利,fig,ax = plt.subplots().
标签:python,python-3-x,matplotlib
来源: https://codeday.me/bug/20190724/1522474.html