简体   繁体   English

“plt.figure”有什么意义?

[英]What's the point of “plt.figure”?

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 根据官方Matplotlib文档( https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.figure )一个数字函数将

"If num is provided, and a figure with this id already exists, make it active, and returns a reference to it. " “如果提供了num,并且已存在具有此id的数字,请将其设为活动状态,并返回对它的引用。”

I tried do the above on my Ipython without plt.figure, but it showed the two required pictures still. 我尝试在没有plt.figure的情况下在我的Ipython上执行上述操作,但它仍然显示了两张所需的图片。

There are three cases where plt.figure is useful: plt.figure有三种情况有用:

  1. Obtaining a handle to a figure. 获取图形的句柄。 In many cases it is useful to have a handle to a figure, ie a variable to store the matplotlib.figure.Figure instance in, such that it can be used later on. 在许多情况下,有一个图形的句柄是有用的,即一个变量来存储matplotlib.figure.Figure实例,以便以后可以使用它。 Example: 例:

     fig = plt.figure() #... other code fig.autofmt_xdate() 
  2. Set figure parameters. 设置图形参数。 An option to set some of the parameters for the figure is to supply them as arguments to plt.figure , eg 为图设置一些参数的选项是将它们作为参数提供给plt.figure ,例如

     plt.figure(figsize=(10,7), dpi=144) 
  3. Create several figures. 创建几个数字。 In order to create several figures in the same script, plt.figure can be used. 为了在同一个脚本中创建多个图形,可以使用plt.figure 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 . 在许多其他情况下,实际上不需要使用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() . 使用pyplot接口,调用任何绘图命令就足以创建一个图形,并且您始终可以使用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() . 在这种情况下,使用plt.subplots更有利, fig, ax = plt.subplots()

The plt.figure gives you a new figure, and depending on the given argument it opens a new figure or not. plt.figure为您提供了一个新的数字,根据给定的参数,它会打开一个新的数字。 Compare: 相比:

plt.figure(1)
plt.plot(x1, y1)
plt.plot(x2, y2)

to

plt.figure(1)
plt.plot(x1, y1)
plt.figure(2)
plt.plot(x2, y2)

to

plt.figure(1)
plt.plot(x1, y1)
plt.figure(1)
plt.plot(x2, y2)

You will see that the first example is equal to the third, because you retrieve the same figure handle with the second plt.figure(1) call in the third example. 您将看到第一个示例等于第三个示例,因为您在第三个示例中使用第二个plt.figure(1)调用检索相同的图形句柄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM