为什么不能将单个椭圆(面片)添加到多个轴?

Why can't I add a single Ellipse (patch) to more than one axis?

为什么我需要为每个轴添加一个新的补丁实例?补丁什么时候依赖于一个轴,为什么不能将数学定义添加到不同的轴上多次绘制? oneEllipse 是处理这个问题的有效方法吗?

如果椭圆是当前轴上的艺术家(如果未指定),为什么我可以在下面添加 f, g 到不同的轴,当它们是使用相同的当前轴创建的?

import numpy as np
import  matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

x, y, width, height, angle=10.0, 6.0, 7.0, 3.0, 160.0

fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

def oneEllipse():
    return Ellipse(xy=(x,y), width=height, height=width, angle=angle,
            facecolor='blue')

e = Ellipse(xy=(x,y), width=width, height=height, angle=angle,
            facecolor='black')

f = Ellipse(xy=(x+2,y+2), width=width, height=height, angle=angle,
            facecolor='green')
g = Ellipse(xy=(x+2,y+2), width=width, height=height, angle=angle,
            facecolor='green')


#ax1.add_artist(e) # Can use e only once --
ax2.add_artist(e) # if added twice, appears on neither axis
                  # whether called in loop or not

ax1.add_artist(f) # These were defined with the same gca(), weren't they?
ax2.add_artist(g) # Why not the same behavior as e?

for ax in (ax1, ax2):
    ax.set_aspect('equal')
    ax.set(xlim=(0,20), ylim=(0,10))
    ax.add_artist(oneEllipse()) # Generating a new Ellipse each time, fine
    ax.add_artist(e) # if e was added to the last axis in the loop *only*,
                     # it appears if in loop

编辑添加:Adding the same Patch instance to multiple subplots in matplotlib 的副本,但有更多失败的例子。

简短的回答是因为你做不到。

稍微长一点的答案是,由于变换堆栈的工作方式,作为绘图过程的一部分,艺术家需要知道 Axes 他们属于什么。进入更多细节需要深入研究完整的 draw/transform 堆栈。

在 mpl 的下一个功能版本(2.1 ~ 2015 年夏季)中尝试这样做会引发异常 (https://github.com/matplotlib/matplotlib/pull/3835)

此外,请注意 AxisAxes (see diagram)