Jia*_*ang 4 python matplotlib python-3.x
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()
Run Code Online (Sandbox Code Playgroud)
据官方Matplotlib文件(https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.figure)一个图形功能
"如果提供了num,并且已存在具有此id的数字,请将其设为活动状态,并返回对它的引用."
我尝试在没有plt.figure的情况下在我的Ipython上执行上述操作,但它仍然显示了两张所需的图片.
为plt.figure
您提供一个新图形,并根据给定的参数打开或不打开一个新图形。比较:
plt.figure(1)
plt.plot(x1, y1)
plt.plot(x2, y2)
Run Code Online (Sandbox Code Playgroud)
到
plt.figure(1)
plt.plot(x1, y1)
plt.figure(2)
plt.plot(x2, y2)
Run Code Online (Sandbox Code Playgroud)
到
plt.figure(1)
plt.plot(x1, y1)
plt.figure(1)
plt.plot(x2, y2)
Run Code Online (Sandbox Code Playgroud)
您将看到第一个示例等于第三个示例,因为您plt.figure(1)
在第三个示例中通过第二次调用检索到相同的图窗句柄。
有三种情况plt.figure
有用:
获取图形的句柄. 在许多情况下,有一个图形的句柄,即一个用于存储matplotlib.figure.Figure
实例的变量,以便以后可以使用它是很有用的.例:
fig = plt.figure()
#... other code
fig.autofmt_xdate()
Run Code Online (Sandbox Code Playgroud)设置图形参数. 为图设置一些参数的选项是将它们作为参数提供给plt.figure
,例如
plt.figure(figsize=(10,7), dpi=144)
Run Code Online (Sandbox Code Playgroud)创建几个数字.为了在同一个脚本中创建几个图形,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
Run Code Online (Sandbox Code Playgroud)在许多其他情况下,实际上根本不需要使用plt.figure
.使用pyplot接口,任何绘图命令拨打电话就足以创造一个人物,你可以随时获取句柄与目前的数字plt.gcf()
.
从另一个角度来看,通常不仅需要具有图形的手柄而且还需要具有要绘制的轴.在这种情况下,使用plt.subplots
更有利,fig, ax = plt.subplots()
.
归档时间: |
|
查看次数: |
1745 次 |
最近记录: |