如何修改 Matplotlib 中现有的图形实例?
在Python中matplotlib.pyplot.figure()用于修改现有的 Figure 实例或创建一个新的 Figure 实例。通常,它用于更改现有图的基本属性。如果它被用来改变已经形成的图的属性,它会引用相关的图。它返回 Figure 实例并将其传递给后端的 new_figure_manager ,作为回报,它允许将 Figure 类自定义挂钩到 pyplot 接口中。
Syntax: matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None,
frameon=True, FigureClass=
Parameters
num : It is a unique identifier for the figure. If the Figure instance is already there with the same value, that instance is returned else a new instance is created and random number assigned to it. In case the value is number then it acts as a unique id and in case of string it also acts as Image label. This parameter is optional.
figsize: It is a parameter which takes in a tuple or a list of 2 floats mentioning the width and height of the figure respectively. By default it is set as [6.4, 4.8]
dpi: As the name suggest, it takes in the dots per inch as a float value. Default value is 100.
facecolor : It is used to set the background color of the figure. Default value is ‘white’.
edgecolor: It is used to set the border color of the figure. Default value is ‘white’.
FigureClass: Takes in the subclass of Figure Optionally used for referring any custom Figure instance.
clear: used to clear any existing figure on the respective instance.
Return: instance.close()
It is recommended to use the .close() method after using figure to use a clean the unnecessary memory. so to close the figure instance, write.
示例 1:使用 figure() 提取 Figure 实例
下面提到的示例显示了 figure 方法如何用于标记任何绘图,并使用相同的标签作为参考点来获取 Figure 实例。资产声明确认了两个实例都指向同一个参考点的事实。
Python3
import matplotlib.pyplot as plt
# Figure instance with label => lebel
fig = plt.figure( num = 'label' )
fig.get_label()
# This will fetch Figure instance fig only
fig2 = plt.figure( num = 'label' )
assert fig == fig2
Python3
import matplotlib.pyplot as plt
# plotting a yellow background
# graph with dpi => 50
plt.figure(num='label',
facecolor='yellow',
figsize=[10, 7],
dpi=50)
plt.plot([2.5, 1, 2.5, 4, 2.5],
[1, 2.5, 4, 2.5, 1])
Python3
import matplotlib.pyplot as plt
plt.plot([2.5, 1, 2.5, 4, 2.5],
[1, 2.5, 4, 2.5, 1])
plt.plot([1, 2, 3, 4], [1, 2, 3, 4])
Python3
import matplotlib.pyplot as plt
plt.plot([2.5, 1, 2.5, 4, 2.5],
[1, 2.5, 4, 2.5, 1])
# This will clear the first plot
plt.figure(clear=True)
# This will make a new plot on a
# different instance
plt.plot([1, 2, 3, 4], [1, 2, 3, 4])
Python3
import matplotlib.pyplot as plt
# the type comes out as Figure Instance.
print(type(plt.figure()))
示例 2:绘制具有自定义高度、宽度和背景的图形
此示例说明如何使用任何绘图的自定义大小并使用自定义 dpi 绘制图形。这里的背景也从白色变成了黄色。高度设置为 10,宽度设置为 7。
蟒蛇3
import matplotlib.pyplot as plt
# plotting a yellow background
# graph with dpi => 50
plt.figure(num='label',
facecolor='yellow',
figsize=[10, 7],
dpi=50)
plt.plot([2.5, 1, 2.5, 4, 2.5],
[1, 2.5, 4, 2.5, 1])
输出
示例 3:清除图形的示例
这里的第一个代码是显示如果在单个实例上绘制两个不同的图而不使用 clear 代码将如何。在这里工作的理想情况是它将两者都绘制在一个图形上。
蟒蛇3
import matplotlib.pyplot as plt
plt.plot([2.5, 1, 2.5, 4, 2.5],
[1, 2.5, 4, 2.5, 1])
plt.plot([1, 2, 3, 4], [1, 2, 3, 4])
输出
现在执行相同的代码,但在实施第二个图之前清除第一个图。
蟒蛇3
import matplotlib.pyplot as plt
plt.plot([2.5, 1, 2.5, 4, 2.5],
[1, 2.5, 4, 2.5, 1])
# This will clear the first plot
plt.figure(clear=True)
# This will make a new plot on a
# different instance
plt.plot([1, 2, 3, 4], [1, 2, 3, 4])
输出
示例 4:检查返回类型
figure() 返回一个 Figure 实例,下一个示例只是验证了这一事实。
蟒蛇3
import matplotlib.pyplot as plt
# the type comes out as Figure Instance.
print(type(plt.figure()))
输出: