Matplotlib 简明教程

Matplotlib - Artists

Understanding Artists in Matplotlib

在 Matplotlib 中,你几乎在绘图上看到的所有内容都是 Artist 的实例,它们是表示绘图的各个组成部分的对象。无论是表示数据的线条、文本标签,甚至是轴上的刻度线,Matplotlib 绘图中的所有内容都是 Artist 对象。

In Matplotlib, almost everything you see on a plot is an instance of an Artist, which are objects that represent various components of a plot. Whether it’s a line representing data, a text label, or even the tick marks on an axis, everything in a Matplotlib plot is an Artist object.

在探讨艺术家层级之前,让我们先观察以下图片以了解 matplotlib 中图形的基本组件 −

Before exploring the Artist hierarchy, let’s observe the below image to understand the basic components of a figure in matplotlib −

artists input

在图片中,每个元素(例如表示数据的线和点,以及次刻度线和点关联文本标签)都可以识别为单个艺术家对象。

In the figure, each element, such as lines and points that represent data, along with minor ticks and points associated text labels, can be identified as individual Artist objects.

Artist Hierarchy

在 matplotlib 的层级中,艺术家可以广泛分为两种类型 −

In matplotlib’s hierarchy, Artists can be broadly categorized into two types −

  1. Primitive Artists − These are the basic building blocks of a plot. Examples include Line2D, Rectangle, Text, AxesImage, and more. These are the standard graphical objects that we want to paint onto our canvas.

  2. Composite Artists − These are higher-level structures that contain other Artists. Examples include Figure, Axis, and Axes. A Figure is like a canvas that holds everything, while Axes represents a specific plot within the figure.

Creating and Accessing the Artists

艺术家通常通过 Axes 对象上的绘图方法创建。这些方法不仅促进了可视化元素的创建,还返回具体艺术家实例以对应于绘图元素。

Artists are typically created through plotting methods on an Axes object. These methods not only facilitate the creation of visual elements but also return specific artist instances corresponding to the plotted elements.

以下是一些常见的绘图方法以及它们生成的对应艺术家 −

Below are some common plotting methods and the corresponding artists they generate −

  1. annotate − Generates an Annotation artist for text annotations.

  2. bar − Creates a Rectangle artist for bar charts.

  3. errorbar − Produces Line2D and Rectangle artists for error bar plots.

  4. fill − Generates a Polygon artist for shared areas.

  5. hist − Creates Rectangle artists for histograms.

  6. imshow − Generates an AxesImage artist for image data.

  7. legend − Produces a Legend artist for Axes legends.

  8. plot − Creates Line2D artists for XY plots. Returns a list when multiple datasets are plotted.

  9. scatter − Generates a PolyCollection artist for scatter charts.

  10. text − Produces a Text artist for displaying text on the plot.

Example

让我们看看使用 plot() 方法创建和访问艺术家的示例。

Let’s see an example for creating and accessing the artists using the plot() method.

import matplotlib.pyplot as plt
import matplotlib.artist as martist
import numpy as np

# Create a subplot
fig, ax = plt.subplots()

# Generate random data
x, y = np.random.rand(2, 100)

# Use the plot method to create a Line2D artist
lines = ax.plot(x, y, '-', label='example')

# Accessing the Line2D artists created by the plot method
print('Line2D artists created by the plot method:', lines)

# Retrieve all Line2D artists associated with the Axes
lines_on_axes = ax.get_lines()

# Display the retrieved Line2D artists
print('Line2D artists obtained using get_lines():', lines_on_axes)

# Accessing the first (and in this case, the only) Line2D artist
print('Accessing the first Line2D artist', ax.get_lines()[0])

执行上述代码,我们将得到以下输出 −

On executing the above code we will get the following output −

Line2D artists created by the plot method: [<matplotlib.lines.Line2D object at 0x000001DB3666A620>]
Line2D artists obtained using get_lines():
Accessing the first Line2D artist Line2D(example)