Matplotlib 简明教程

Matplotlib - Anchored Artists

在 Matplotlib 中, Artist 是表示绘图中几乎所有组件的基本对象。无论是线、文本、轴还是任何其他图形元素,Matplotlib 绘图中的所有内容都是 Artist 的实例或从 Artist 类派生而来。

In Matplotlib, an Artist is a fundamental object that represents almost all components of a plot. Whether it’s a line, text, axis, or any other graphical element, everything in a Matplotlib plot is an instance of an Artist or is derived from the Artist class.

Anchored Artists 是自定义 artist 的特殊类型,可以锚定到绘图上的特定位置。它们可用于添加注释、箭头和其他锚定在特定点或区域上的自定义元素。

Anchored Artists are a special type of custom artist that can be anchored to specific positions on a plot. They are useful for adding annotations, arrows, and other custom elements that are anchored to a particular point or region.

请参见下面的参考示例 −

See the below example for refence −

anchored artists intro

在上图中,您可以观察到文本框、圆环和大小栏被固定在绘图上的特定位置。

In the above image, you can observe that text boxes, circles, and size bars are anchored at specific positions on the plot.

Anchored Artists in Matplotlib

在 Matplotlib 中有两个提供固定化对象的模块,分别是 −

There are two modules that provides Anchored Artists in Matplotlib, which are −

  1. Matplotlib.offsetbox

  2. Mpl_toolkits.axes_grid1.anchored_artists

The matplotlib.offsetbox module

此模块提供了 AnchoredOffsetboxAnchoredText 类,允许您相对于父轴或特定锚点固定任意的对象或文本。它们可用于更通用的注释和修饰。

This module provides classes like AnchoredOffsetbox and AnchoredText, allowing you to anchor arbitrary artists or text relative to the parent axes or a specific anchor point. These can be used for more general-purpose annotations and decorations.

现在,我们使用 matplotlib.offsetbox 模块中的 AnchoredText 类在绘图上的特定位置实现两个 anchored text boxes

Now, let’s use the AnchoredText class from the matplotlib.offsetbox module to implement two anchored text boxes at specific location on the plot.

import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredText

# Create a figure and axis
fig, ax = plt.subplots(figsize=(7, 4))

# Anchored Text Box 1
at = AnchoredText("Anchored text-box 1",
   loc='upper left', prop=dict(size=10), frameon=True)
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(at)

# Anchored Text Box 2
at2 = AnchoredText("Anchored text-box 2",
   loc='center', prop=dict(size=16), frameon=True,
   bbox_to_anchor=(0.5, 0.5),
   bbox_transform=ax.transAxes)
at2.patch.set_boxstyle("round,pad=0.,rounding_size=0.5")
ax.add_artist(at2)

# Display the plot
plt.show()

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

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

anchored artists ex1

The mpl_toolkits.axes_grid1.anchored_artists module

此模块提供了 AnchoredDirectionArrowsAnchoredAuxTransformBoxAnchoredDrawingAreaAnchoredSizeBar 等特殊固定化对象。该类的每一个都用于不同的目的。

This module offers specialized Anchored Artists like AnchoredDirectionArrows, AnchoredAuxTransformBox, AnchoredDrawingArea, and AnchoredSizeBar. Eachof this class is used to different porposes.

我们来看每个类的用法 −

Let’s see the usase of the each class −

  1. AnchoredAuxTransformBox − An anchored container with transformed coordinates.

  2. AnchoredDirectionArrows − Draw two perpendicular arrows to indicate directions.

  3. AnchoredDrawingArea − An anchored container with a fixed size and fillable DrawingArea.

  4. AnchoredSizeBar − Draw a horizontal scale bar with a center-aligned label underneath.

本示例演示如何使用 AnchoredDirectionArrows 类将视觉上吸引人的固定化方向箭头添加到 Matplotlib 绘图。

This example demonstrates how to use AnchoredDirectionArrows class to add a visually appealing anchored direction arrow to the Matplotlib plot.

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as fm
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredDirectionArrows

np.random.seed(19680801)

fig, ax = plt.subplots(figsize=(7, 4))
ax.imshow(np.random.random((10, 10)))

# Rotated arrow
fontprops = fm.FontProperties(family='serif')

rotated_arrow = AnchoredDirectionArrows(
   ax.transAxes,
   '30', '120',
   loc='center',
   color='w',
   angle=30,
   fontproperties=fontprops
)
ax.add_artist(rotated_arrow)

plt.show()

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

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

anchored artists ex2