Matplotlib 简明教程
Matplotlib - Figure Class
在 Matplotlib 中, Figure 是一个顶级容器,其中包含绘图或可视化的所有元素。它是一个包含各种组件(例如坐标轴、标记、标题、图例、色带和其他元素)的总体窗口或画布。
In Matplotlib, a Figure is a top-level container that holds all the elements of a plot or visualization. It is an overall window or canvas that contains various components like axes, labels, titles, legends, colorbars, and other elements.
参阅以下示例图像 −
See the below image for reference −
在上图中,绿色区域描绘出图形,白色区域是坐标轴区域。
In the above image, the green region represents the figure and the white region is the axes area.
Figure Class in Matplotlb
Matplotlib 中的 Figure() 类是一个顶级 Artist,作为所有绘图元素的主要容器。它将所有内容组合在一起,包括子图、坐标轴、标题、图例及其他艺术元素。
The Figure() class in Matplotlib is a top-level artist that acts as the primary container for all plot elements. It holds everything together, including subplots, axes, titles, legends, and other artistic elements.
这个类在 matplotlib.figure 模块中有提供,带有几个自定义选项,除了 Figure() 类以外,该模块还包含与创建和管理图形相关联的类。
This class is available in the matplotlib.figure module with several customization options, in addition to the Figure() class, the module also contains classes related to creating and managing figures.
Creating a Figure
图例实例通常使用 pyplot 方法(例如 figure、subplots 和 subplot_mosaic)创建。这些方法会返回图例实例和一组轴,提供一种创建和处理可视化的便捷方式。
A Figure instance is typically created using pyplot methods such as figure, subplots, and subplot_mosaic. These methods return both a Figure instance and a set of Axes, providing a convenient way to create and work with visualizations.
以下是一个使用 pyplot.figure() 方法创建图例的示例。
Here is an example that uses the pyplot.figure() method to create a figure.
import matplotlib.pyplot as plt
import numpy as np
# Creating the Figure instance
fig = plt.figure(figsize=[7, 3], facecolor='lightgreen', layout='constrained')
# Adding a title to the Figure
fig.suptitle('Figure')
# Adding a subplot (Axes) to the Figure
ax = fig.add_subplot()
# Setting a title for the subplot
ax.set_title('Axes', loc='left', fontstyle='oblique', fontsize='medium')
# Showing the plot
plt.show()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
此示例演示如何在单个 Matplotlib 脚本中分别创建多个图例。
This example demonstrates how to create multiple figures separately within a single script in Matplotlib.
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create Figure 1
fig1 = plt.figure("Figure 1")
plt.plot([1, 3, 7, 3, 1], c="red", lw=2)
plt.title("Figure 1")
# Create Figure 2
fig2 = plt.figure("Figure 2")
plt.plot([1, 3, 7, 3, 1], c="green", lw=5)
plt.title("Figure 2")
# Display both figures
plt.show()
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −
我无法使用 Gemini 翻译任何内容。
Creating a Figure with Grids of Subplots
当绘制图形时,可以自定义多种选项,包括子图、大小、分辨率、颜色和布局。Figure 类属性,如 figsize、dpi、facecolor、edgecolor、linewidth 和 layout,在塑造可视化的外观方面发挥着至关重要的作用。
When creating figures, various options can be customized, including subplots, size, resolution, colors, and layout. The Figure class attributes such as figsize, dpi, facecolor, edgecolor, linewidth, and layout play crucial roles in shaping the appearance of your visualizations.
Example
这里是一个使用 pyplot.subplots() 方法来创建具有多个不同自定义选项的 2x2 网格子图的示例。
Here is an example that uses the pyplot.subplots() method to create a 2x2 grid of subplots with multiple various customization options.
import matplotlib.pyplot as plt
import numpy as np
# Create a 2x2 grid of subplots with various customization options
fig, axs = plt.subplots(2, 2, figsize=(7, 4), facecolor='lightgreen',
layout='constrained')
# Super title for the entire figure
fig.suptitle('2x2 Grid of Subplots', fontsize='x-large')
# Display the Figure
plt.show()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
Example
这里有另一个使用 plt.subplot_mosaic() 方法创建更复杂布局的示例。
Here is another example that creates a more complex layout using the plt.subplot_mosaic() method.
import matplotlib.pyplot as plt
# Create a more complex layout using plt.subplot_mosaic()
fig, axs = plt.subplot_mosaic([['A', 'right'], ['B', 'right']],
facecolor='lightgreen',
layout='constrained')
# Add text to each subplot
for ax_name, ax in axs.items():
ax.text(0.5, 0.5, ax_name, ha='center', va='center',
fontsize='large', fontweight='bold', color='blue')
# Super title for the entire figure
fig.suptitle('Complex Layout using subplot_mosaic()', fontsize='x-large')
# Display the Figure
plt.show()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
Saving a Figure
完成可视化后,使用 savefig() 方法可以轻松地将图形保存到磁盘。此方法允许您指定文件格式(例如 PNG、PDF),并自定义分辨率和边界框等选项。
After completing a visualization, saving Figures to disk is simple using the savefig() method. This method allows you to specify the file format (e.g., PNG, PDF) and customize options such as resolution and bounding box.
Example
让我们看一个保存 Figure 对象的简单示例。
Let’s see a simple example to save the Figure object.
import matplotlib.pyplot as plt
# Create a 2x2 grid of subplots with various customization options
fig, axs = plt.subplots(2, 2, figsize=(7, 4), facecolor='lightgreen',
layout='constrained')
# Super title for the entire figure
fig.suptitle('2x2 Grid of Subplots', fontsize='x-large')
# Super title for the entire figure
fig.suptitle('Saving a Figure', fontsize='x-large')
# Display the Figure
plt.show()
# Save the Figure object to a file
fig.savefig('Saved Figure.png', dpi=300)
在执行以上程序时,以下图形将保存在你的工作目录中 -
On executing the above program, the following figure will be saved in your working direcory −