默认 Matplotlib 轴子项

Default Matplotlib Axes children

我只想了解

的最后4个条目是什么
plt.gca().get_children()

是。我知道第一个文本是情节标签,但不知道其他两个是什么。我看过其他地方,似乎它们总是位于 0,1 和 1,1。最后,我假设最后一个矩形实际上是 Axes 中所有内容 placed/plotted 的“表面”。

[<matplotlib.lines.Line2D at 0x7fa18484dba8>,
 <matplotlib.spines.Spine at 0x7fa1848992b0>,
 <matplotlib.spines.Spine at 0x7fa1848994e0>,
 <matplotlib.spines.Spine at 0x7fa1848996d8>,
 <matplotlib.spines.Spine at 0x7fa1848998d0>,
 <matplotlib.axis.XAxis at 0x7fa184899a90>,
 <matplotlib.axis.YAxis at 0x7fa184824860>,
 <matplotlib.text.Text at 0x7fa1848c2a58>,
 <matplotlib.text.Text at 0x7fa1848c2c50>,
 <matplotlib.text.Text at 0x7fa1848c24e0>,
 <matplotlib.patches.Rectangle at 0x7fa1848c2518>]

我在这里看到了这方面的例子 (https://liujing.neocities.org/newworld/datascience/Python/plotting/1.%20Basic%20Plotting%20with%20matplotlib.html), and here (https://www.google.com/amp/s/www.geeksforgeeks.org/matplotlib-axes-axes-get_children-in-python/amp/ )

Matplotlib 支持每个子图有 3 个标题。默认的标题是居中的,但在左边加一个标题and/or在右边可以设置。 (在内部,这两个标题将通过私有属性 _left_title_right_title 访问。设置这些标题的正确方法是 ax.set_title(..., loc='left')plt.title(..., loc=...)

children 的最后一个是背景矩形。它通常作为 ax.patch 访问,可以用于更改背景。

以下代码说明了这些概念:

import matplotlib.pyplot as plt

plt.title('central title', size=14)
plt.title('⟵ left title', loc='left')
plt.title('right title ⟶', loc='right')
plt.gca().patch.set_hatch('xx')
plt.gca().patch.set_edgecolor('NavajoWhite')
plt.gca().get_children()[-1].set_facecolor('LemonChiffon')
for child in plt.gca().get_children():
    print(child)

输出:

Spine
Spine
Spine
Spine
XAxis(80.0,52.8)
YAxis(80.0,52.8)
Text(0.5, 1.0, 'central title')
Text(0.0, 1.0, '⟵ left title')
Text(1.0, 1.0, 'right title ⟶')
Rectangle(xy=(0, 0), width=1, height=1, angle=0)