备注
前往末尾 下载完整示例代码。
注解#
注释是图形元素,通常是文本片段,用于解释、添加上下文或突出显示可视化数据的一部分。annotate 支持多种坐标系,以便灵活地定位数据和注释相对于彼此的位置,并提供多种选项来设置文本样式。Axes.annotate 还提供了一个可选的箭头,从文本指向数据,这个箭头可以以多种方式进行样式设置。text 也可以用于简单的文本注释,但在定位和样式设置方面的灵活性不如 annotate。
基本注释#
在注释中,有两个点需要考虑:被注释数据的位置 xy 和注释文本的位置 xytext。这两个参数都是 (x, y) 元组:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(3, 3))
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = ax.plot(t, s, lw=2)
ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
arrowprops=dict(facecolor='black', shrink=0.05))
ax.set_ylim(-2, 2)

在这个例子中,xy*(箭头尖端)和 *xytext 位置(文本位置)都在数据坐标中。还有多种其他坐标系可供选择——您可以使用以下字符串之一为 xycoords 和 textcoords 指定 xy 和 xytext 的坐标系(默认是 'data')
参数 |
坐标系 |
|---|---|
'figure points' |
从图的左下角开始计算的点 |
'figure 像素' |
从图形的左下角开始的像素 |
'figure fraction' |
(0, 0) 是图形的左下角,(1, 1) 是图形的右上角 |
'轴点' |
从Axes的左下角开始的点 |
'axes pixels' |
从Axes的左下角开始的像素 |
'axes fraction' |
(0, 0) 是 Axes 的左下角,(1, 1) 是右上角 |
'数据' |
使用轴数据坐标系 |
以下字符串也是 textcoords 的有效参数
参数 |
坐标系 |
|---|---|
'偏移点' |
从 xy 值的偏移量(以点为单位) |
'偏移像素' |
从 xy 值的偏移量(以像素为单位) |
对于物理坐标系(点或像素),原点是图形或轴的左下角。点是 排版点 ,这意味着它们是测量1/72英寸的物理单位。点和像素在 在物理坐标中绘图 中有更详细的讨论。
注释数据#
此示例将文本坐标放置在分数轴坐标中:
fig, ax = plt.subplots(figsize=(3, 3))
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = ax.plot(t, s, lw=2)
ax.annotate('local max', xy=(2, 1), xycoords='data',
xytext=(0.01, .99), textcoords='axes fraction',
va='top', ha='left',
arrowprops=dict(facecolor='black', shrink=0.05))
ax.set_ylim(-2, 2)

注释艺术家#
通过将该艺术家实例作为 xycoords 传递,注释可以相对于 Artist 实例定位。然后 xy 被解释为艺术家边界框的一部分。
import matplotlib.patches as mpatches
fig, ax = plt.subplots(figsize=(3, 3))
arr = mpatches.FancyArrowPatch((1.25, 1.5), (1.75, 1.5),
arrowstyle='->,head_width=.15', mutation_scale=20)
ax.add_patch(arr)
ax.annotate("label", (.5, .5), xycoords=arr, ha='center', va='bottom')
ax.set(xlim=(1, 2), ylim=(1, 2))

这里注释放置在相对于箭头左下角 (.5,.5) 的位置,并且在该位置垂直和水平对齐。垂直方向上,底部与该参考点对齐,因此标签位于线的上方。有关链接注释艺术家的示例,请参见 坐标系注释 的 艺术家部分。
使用箭头进行标注#
你可以通过在可选的关键字参数 arrowprops 中提供一个箭头属性的字典来启用从文本到注释点的箭头绘制。
arrowprops 键 |
描述 |
|---|---|
宽度 |
箭头的宽度,以点为单位 |
frac |
箭头头部占据箭头长度的比例 |
headwidth |
箭头头部的基部宽度,以点为单位 |
收缩 |
将尖端和基部移动到注释点和文本的某个百分比之外 |
**kwargs |
任何 |
在下面的例子中,xy 点位于数据坐标系中,因为 xycoords 默认值为 'data'。对于极坐标轴,这是在 (theta, 半径) 空间中。这个例子中的文本被放置在分数图形坐标系中。matplotlib.text.Text 关键字参数,如 horizontalalignment、verticalalignment 和 fontsize,从 annotate 传递到 Text 实例。
fig = plt.figure()
ax = fig.add_subplot(projection='polar')
r = np.arange(0, 1, 0.001)
theta = 2 * 2*np.pi * r
line, = ax.plot(theta, r, color='#ee8d18', lw=3)
ind = 800
thisr, thistheta = r[ind], theta[ind]
ax.plot([thistheta], [thisr], 'o')
ax.annotate('a polar annotation',
xy=(thistheta, thisr), # theta, radius
xytext=(0.05, 0.05), # fraction, fraction
textcoords='figure fraction',
arrowprops=dict(facecolor='black', shrink=0.05),
horizontalalignment='left',
verticalalignment='bottom')

有关使用箭头绘图的更多信息,请参阅 自定义注释箭头
相对于数据放置文本注释#
可以通过将 textcoords 关键字参数设置为 'offset points' 或 'offset pixels',将注释定位在相对于 xy 输入的相对偏移位置。
fig, ax = plt.subplots(figsize=(3, 3))
x = [1, 3, 5, 7, 9]
y = [2, 4, 6, 8, 10]
annotations = ["A", "B", "C", "D", "E"]
ax.scatter(x, y, s=20)
for xi, yi, text in zip(x, y, annotations):
ax.annotate(text,
xy=(xi, yi), xycoords='data',
xytext=(1.5, 1.5), textcoords='offset points')

注释从 xy 值偏移 1.5 点(1.5*1/72 英寸)。
高级注解#
我们建议在阅读本节之前先阅读 基本注释、text() 和 annotate()。
使用带框文本进行注释#
text 接受一个 bbox 关键字参数,该参数在文本周围绘制一个框:
fig, ax = plt.subplots(figsize=(5, 5))
t = ax.text(0.5, 0.5, "Direction",
ha="center", va="center", rotation=45, size=15,
bbox=dict(boxstyle="rarrow,pad=0.3",
fc="lightblue", ec="steelblue", lw=2))

参数是带有其属性作为关键字参数的框样式名称。目前,实现了以下框样式:
类 |
名称 |
Attrs |
|---|---|---|
圆 |
|
pad=0.3 |
DArrow |
|
pad=0.3 |
椭圆 |
|
pad=0.3 |
LArrow |
|
pad=0.3 |
RArrow |
|
pad=0.3 |
圆 |
|
pad=0.3,rounding_size=None |
第四轮 |
|
pad=0.3,rounding_size=None |
圆齿 |
|
pad=0.3,tooth_size=None |
锯齿 |
|
pad=0.3,tooth_size=None |
正方形 |
|
pad=0.3 |
与文本关联的补丁对象(框)可以使用以下方式访问:
bb = t.get_bbox_patch()
返回值是一个 FancyBboxPatch;补丁属性(如 facecolor、edgewidth 等)可以像往常一样访问和修改。FancyBboxPatch.set_boxstyle 设置盒子的形状:
bb.set_boxstyle("rarrow", pad=0.6)
属性参数也可以在样式名称中用逗号分隔指定:
bb.set_boxstyle("rarrow, pad=0.6")
定义自定义盒子样式#
自定义框样式可以实现为一个函数,该函数接受指定矩形框和“变异”量的参数,并返回“变异”后的路径。具体签名是下面 custom_box_style 的签名。
在这里,我们返回一个新的路径,它在盒子的左侧添加了一个“箭头”形状。
然后可以通过将 bbox=dict(boxstyle=custom_box_style, ...) 传递给 Axes.text 来使用自定义框样式。
from matplotlib.path import Path
def custom_box_style(x0, y0, width, height, mutation_size):
"""
Given the location and size of the box, return the path of the box around it.
Rotation is automatically taken care of.
Parameters
----------
x0, y0, width, height : float
Box location and size.
mutation_size : float
Mutation reference scale, typically the text font size.
"""
# padding
mypad = 0.3
pad = mutation_size * mypad
# width and height with padding added.
width = width + 2 * pad
height = height + 2 * pad
# boundary of the padded box
x0, y0 = x0 - pad, y0 - pad
x1, y1 = x0 + width, y0 + height
# return the new path
return Path([(x0, y0), (x1, y0), (x1, y1), (x0, y1),
(x0-pad, (y0+y1)/2), (x0, y0), (x0, y0)],
closed=True)
fig, ax = plt.subplots(figsize=(3, 3))
ax.text(0.5, 0.5, "Test", size=30, va="center", ha="center", rotation=30,
bbox=dict(boxstyle=custom_box_style, alpha=0.2))

同样,自定义的盒子样式可以通过实现 __call__ 的类来实现。
然后可以将这些类注册到 BoxStyle._style_list 字典中,这允许将框样式指定为字符串,bbox=dict(boxstyle="registered_name,param=value,...", ...)。请注意,此注册依赖于内部API,因此不受官方支持。
from matplotlib.patches import BoxStyle
class MyStyle:
"""A simple box."""
def __init__(self, pad=0.3):
"""
The arguments must be floats and have default values.
Parameters
----------
pad : float
amount of padding
"""
self.pad = pad
super().__init__()
def __call__(self, x0, y0, width, height, mutation_size):
"""
Given the location and size of the box, return the path of the box around it.
Rotation is automatically taken care of.
Parameters
----------
x0, y0, width, height : float
Box location and size.
mutation_size : float
Reference scale for the mutation, typically the text font size.
"""
# padding
pad = mutation_size * self.pad
# width and height with padding added
width = width + 2 * pad
height = height + 2 * pad
# boundary of the padded box
x0, y0 = x0 - pad, y0 - pad
x1, y1 = x0 + width, y0 + height
# return the new path
return Path([(x0, y0), (x1, y0), (x1, y1), (x0, y1),
(x0-pad, (y0+y1)/2), (x0, y0), (x0, y0)],
closed=True)
BoxStyle._style_list["angled"] = MyStyle # Register the custom style.
fig, ax = plt.subplots(figsize=(3, 3))
ax.text(0.5, 0.5, "Test", size=30, va="center", ha="center", rotation=30,
bbox=dict(boxstyle="angled,pad=0.5", alpha=0.2))
del BoxStyle._style_list["angled"] # Unregister it.

同样地,你可以定义一个自定义的 ConnectionStyle 和自定义的 ArrowStyle。查看 patches 的源代码以了解每个类的定义方式。
自定义注释箭头#
通过指定 arrowprops 参数,可以可选地绘制从 xy 到 xytext 的箭头。要仅绘制箭头,请使用空字符串作为第一个参数:
fig, ax = plt.subplots(figsize=(3, 3))
ax.annotate("",
xy=(0.2, 0.2), xycoords='data',
xytext=(0.8, 0.8), textcoords='data',
arrowprops=dict(arrowstyle="->", connectionstyle="arc3"))

箭头绘制如下:
根据 connectionstyle 参数的指定,创建了一条连接两个点的路径。
路径被裁剪以避免补丁 patchA 和 patchB,如果这些被设置的话。
路径进一步由 shrinkA 和 shrinkB (以像素为单位)缩小。
路径被转换为箭头补丁,如 arrowstyle 参数所指定。
两点之间连接路径的创建由 connectionstyle 键控制,以下样式可用:
名称 |
Attrs |
|---|---|
|
angleA=90,angleB=0,rad=0.0 |
|
angleA=90,angleB=0 |
|
angleA=0, angleB=0, armA=None, armB=None, rad=0.0 |
|
rad=0.0 |
|
armA=0.0,armB=0.0,fraction=0.3,angle=None |
请注意,angle3 和 arc3 中的“3”表示生成的路径是一个二次样条线段(三个控制点)。如下文所述,某些箭头样式选项只能在连接路径为二次样条时使用。
每种连接样式的行为在下例中(有限地)展示。(警告:bar 样式目前的行为尚未明确定义,未来可能会更改)。
(Source code, 2x.png, png)
Connection styles for annotations
连接路径(在裁剪和收缩之后)然后根据给定的 arrowstyle 变异为一个箭头补丁:
名称 |
Attrs |
|---|---|
|
无 |
|
head_length=0.4,head_width=0.2 |
|
widthB=1.0,lengthB=0.2,angleB=None |
|
widthA=1.0,widthB=1.0 |
|
head_length=0.4,head_width=0.2 |
|
head_length=0.4,head_width=0.2 |
|
head_length=0.4,head_width=0.2 |
|
head_length=0.4,head_width=0.2 |
|
head_length=0.4,head_width=0.2 |
|
head_length=0.4,head_width=0.4,tail_width=0.4 |
|
head_length=0.5,head_width=0.5,tail_width=0.2 |
|
tail_width=0.3,shrink_factor=0.5 |
某些箭头样式仅在使用生成二次样条线段的连接样式时有效。它们是 fancy、simple 和 wedge。对于这些箭头样式,您必须使用 "angle3" 或 "arc3" 连接样式。
如果给出了注释字符串,补丁默认设置为文本的 bbox 补丁。
fig, ax = plt.subplots(figsize=(3, 3))
ax.annotate("Test",
xy=(0.2, 0.2), xycoords='data',
xytext=(0.8, 0.8), textcoords='data',
size=20, va="center", ha="center",
arrowprops=dict(arrowstyle="simple",
connectionstyle="arc3,rad=-0.2"))

与 text 一样,可以使用 bbox 参数在文本周围绘制一个框。
fig, ax = plt.subplots(figsize=(3, 3))
ann = ax.annotate("Test",
xy=(0.2, 0.2), xycoords='data',
xytext=(0.8, 0.8), textcoords='data',
size=20, va="center", ha="center",
bbox=dict(boxstyle="round4", fc="w"),
arrowprops=dict(arrowstyle="-|>",
connectionstyle="arc3,rad=-0.2",
fc="w"))

默认情况下,起始点设置为文本范围的中心。这可以通过 relpos 键值进行调整。这些值是相对于文本范围进行归一化的。例如,(0, 0) 表示左下角,(1, 1) 表示右上角。
fig, ax = plt.subplots(figsize=(3, 3))
ann = ax.annotate("Test",
xy=(0.2, 0.2), xycoords='data',
xytext=(0.8, 0.8), textcoords='data',
size=20, va="center", ha="center",
bbox=dict(boxstyle="round4", fc="w"),
arrowprops=dict(arrowstyle="-|>",
connectionstyle="arc3,rad=0.2",
relpos=(0., 0.),
fc="w"))
ann = ax.annotate("Test",
xy=(0.2, 0.2), xycoords='data',
xytext=(0.8, 0.8), textcoords='data',
size=20, va="center", ha="center",
bbox=dict(boxstyle="round4", fc="w"),
arrowprops=dict(arrowstyle="-|>",
connectionstyle="arc3,rad=-0.2",
relpos=(1., 0.),
fc="w"))

将艺术家放置在锚定的轴位置#
在 Axes 中可以放置一些固定位置的艺术家类。一个常见的例子是图例。这种类型的艺术家可以通过使用 OffsetBox 类来创建。在 matplotlib.offsetbox 和 mpl_toolkits.axes_grid1.anchored_artists 中有一些预定义的类可用。
from matplotlib.offsetbox import AnchoredText
fig, ax = plt.subplots(figsize=(3, 3))
at = AnchoredText("Figure 1a",
prop=dict(size=15), frameon=True, loc='upper left')
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(at)

loc 关键字与图例命令中的含义相同。
一个简单的应用场景是,当艺术家(或艺术家集合)的大小在创建时已知为像素大小时。例如,如果你想画一个固定大小为20像素x20像素(半径=10像素)的圆,你可以使用 AnchoredDrawingArea。该实例以绘图区域的大小(以像素为单位)创建,并且可以将任意艺术家添加到绘图区域。请注意,添加到绘图区域的艺术家的大小与绘图区域本身的放置无关。只有初始大小是重要的。
添加到绘图区域的艺术元素不应设置变换(将被覆盖),并且这些艺术元素的尺寸被解释为像素坐标,即,上述示例中圆的半径分别为10像素和5像素。
from matplotlib.patches import Circle
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredDrawingArea
fig, ax = plt.subplots(figsize=(3, 3))
ada = AnchoredDrawingArea(40, 20, 0, 0,
loc='upper right', pad=0., frameon=False)
p1 = Circle((10, 10), 10)
ada.drawing_area.add_artist(p1)
p2 = Circle((30, 10), 5, fc="r")
ada.drawing_area.add_artist(p2)
ax.add_artist(ada)

有时,您希望您的艺术家与数据坐标(或其他非画布像素的坐标)一起缩放。您可以使用 AnchoredAuxTransformBox 类。这与 AnchoredDrawingArea 类似,只是艺术家的范围是在绘图时确定的,并尊重指定的变换。
下面例子中的椭圆将具有对应于数据坐标中0.1和0.4的宽度和高度,并且当Axes的视图限制改变时将自动缩放。
from matplotlib.patches import Ellipse
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredAuxTransformBox
fig, ax = plt.subplots(figsize=(3, 3))
box = AnchoredAuxTransformBox(ax.transData, loc='upper left')
el = Ellipse((0, 0), width=0.1, height=0.4, angle=30) # in data coordinates!
box.drawing_area.add_artist(el)
ax.add_artist(box)

另一种将艺术家相对于父Axes或锚点定位的方法是通过`.AnchoredOffsetbox`的*bbox_to_anchor*参数。然后可以使用`.HPacker`和`.VPacker`自动将此艺术家相对于另一个艺术家定位:
from matplotlib.offsetbox import (AnchoredOffsetbox, DrawingArea, HPacker,
TextArea)
fig, ax = plt.subplots(figsize=(3, 3))
box1 = TextArea(" Test: ", textprops=dict(color="k"))
box2 = DrawingArea(60, 20, 0, 0)
el1 = Ellipse((10, 10), width=16, height=5, angle=30, fc="r")
el2 = Ellipse((30, 10), width=16, height=5, angle=170, fc="g")
el3 = Ellipse((50, 10), width=16, height=5, angle=230, fc="b")
box2.add_artist(el1)
box2.add_artist(el2)
box2.add_artist(el3)
box = HPacker(children=[box1, box2],
align="center",
pad=0, sep=5)
anchored_box = AnchoredOffsetbox(loc='lower left',
child=box, pad=0.,
frameon=True,
bbox_to_anchor=(0., 1.02),
bbox_transform=ax.transAxes,
borderpad=0.,)
ax.add_artist(anchored_box)
fig.subplots_adjust(top=0.8)

注意,与 Legend 不同,bbox_transform 默认设置为 IdentityTransform。
注释的坐标系#
Matplotlib 注释支持多种坐标系统。基本注释 中的示例使用了 data 坐标系统;其他一些更高级的选项包括:
Transform 实例#
变换将坐标映射到不同的坐标系统,通常是显示坐标系统。详见 变换教程 的详细解释。这里变换对象用于标识相应点的坐标系统。例如,Axes.transAxes 变换相对于 Axes 坐标定位注释;因此使用它与将坐标系统设置为“轴分数”相同:
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(6, 3))
ax1.annotate("Test", xy=(0.2, 0.2), xycoords=ax1.transAxes)
ax2.annotate("Test", xy=(0.2, 0.2), xycoords="axes fraction")

另一个常用的 Transform 实例是 Axes.transData。这个变换是 Axes 中绘制的数据的坐标系。在这个例子中,它用于在两个 Axes 中的相关数据点之间绘制箭头。我们传递了一个空的文本,因为在这种情况下,注释连接了数据点。
x = np.linspace(-1, 1)
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(6, 3))
ax1.plot(x, -x**3)
ax2.plot(x, -3*x**2)
ax2.annotate("",
xy=(0, 0), xycoords=ax1.transData,
xytext=(0, 0), textcoords=ax2.transData,
arrowprops=dict(arrowstyle="<->"))

Artist 实例#
xy 值(或 xytext)被解释为艺术家边界框(bbox)的分数坐标:
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(3, 3))
an1 = ax.annotate("Test 1",
xy=(0.5, 0.5), xycoords="data",
va="center", ha="center",
bbox=dict(boxstyle="round", fc="w"))
an2 = ax.annotate("Test 2",
xy=(1, 0.5), xycoords=an1, # (1, 0.5) of an1's bbox
xytext=(30, 0), textcoords="offset points",
va="center", ha="left",
bbox=dict(boxstyle="round", fc="w"),
arrowprops=dict(arrowstyle="->"))

请注意,您必须确保在绘制 an2 之前确定坐标艺术家(本例中为 an1)的范围。通常,这意味着 an2 需要在 an1 之后绘制。所有边界框的基类是 BboxBase
返回 BboxBase 的 Transform 的可调用对象#
一个可调用对象,它以渲染器实例作为唯一参数,并返回一个 Transform 或一个 BboxBase。例如,Artist.get_window_extent 的返回值是一个 bbox,因此这种方法与(2)传入艺术家相同:
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(3, 3))
an1 = ax.annotate("Test 1",
xy=(0.5, 0.5), xycoords="data",
va="center", ha="center",
bbox=dict(boxstyle="round", fc="w"))
an2 = ax.annotate("Test 2",
xy=(1, 0.5), xycoords=an1.get_window_extent,
xytext=(30, 0), textcoords="offset points",
va="center", ha="left",
bbox=dict(boxstyle="round", fc="w"),
arrowprops=dict(arrowstyle="->"))

Artist.get_window_extent 是 Axes 对象的边界框,因此与将坐标系设置为轴分数相同:
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(6, 3))
an1 = ax1.annotate("Test1", xy=(0.5, 0.5), xycoords="axes fraction")
an2 = ax2.annotate("Test 2", xy=(0.5, 0.5), xycoords=ax2.get_window_extent)

混合坐标规范#
一对混合的坐标规范——第一个用于 x 坐标,第二个用于 y 坐标。例如,x=0.5 是数据坐标,而 y=1 是归一化的轴坐标:
fig, ax = plt.subplots(figsize=(3, 3))
ax.annotate("Test", xy=(0.5, 1), xycoords=("data", "axes fraction"))
ax.axvline(x=.5, color='lightgray')
ax.set(xlim=(0, 2), ylim=(1, 2))

在混合规范中可以使用任何支持的坐标系。例如,文本“锚定到1和2”相对于两个`.Text`艺术家定位:
fig, ax = plt.subplots(figsize=(3, 3))
t1 = ax.text(0.05, .05, "Text 1", va='bottom', ha='left')
t2 = ax.text(0.90, .90, "Text 2", ha='right')
t3 = ax.annotate("Anchored to 1 & 2", xy=(0, 0), xycoords=(t1, t2),
va='bottom', color='tab:orange',)

text.OffsetFrom#
有时,您希望注释具有一些“偏移点”,不是从被注释的点,而是从其他点或艺术家。text.OffsetFrom 是这种情况下的一个辅助工具。
from matplotlib.text import OffsetFrom
fig, ax = plt.subplots(figsize=(3, 3))
an1 = ax.annotate("Test 1", xy=(0.5, 0.5), xycoords="data",
va="center", ha="center",
bbox=dict(boxstyle="round", fc="w"))
offset_from = OffsetFrom(an1, (0.5, 0))
an2 = ax.annotate("Test 2", xy=(0.1, 0.1), xycoords="data",
xytext=(0, -10), textcoords=offset_from,
# xytext is offset points from "xy=(0.5, 0), xycoords=an1"
va="top", ha="center",
bbox=dict(boxstyle="round", fc="w"),
arrowprops=dict(arrowstyle="->"))

非文本注释#
使用 ConnectionPatch#
ConnectionPatch 就像一个没有文字的注释。虽然 annotate 在大多数情况下已经足够,但当你想连接不同 Axes 中的点时,ConnectionPatch 就非常有用了。例如,这里我们将 ax1 数据坐标中的点 xy 连接到 ax2 数据坐标中的点 xy:
from matplotlib.patches import ConnectionPatch
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(6, 3))
xy = (0.3, 0.2)
con = ConnectionPatch(xyA=xy, coordsA=ax1.transData,
xyB=xy, coordsB=ax2.transData)
fig.add_artist(con)

在这里,我们将 ConnectionPatch 添加到 图形 (通过 add_artist)而不是添加到任一轴。这确保了 ConnectionPatch 艺术家在两个轴之上绘制,并且在使用 constrained_layout 定位轴时也是必要的。
坐标轴之间的缩放效果#
mpl_toolkits.axes_grid1.inset_locator 定义了一些补丁类,这些类对于连接两个 Axes 非常有用。
脚本的总运行时间: (0 分钟 1.392 秒)