为什么我不能将单个Ellipse(补丁)添加到多个轴?

时间:2015-03-24 22:25:36

标签: python matplotlib

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

如果Ellipse是当前轴上的艺术家(如果未指定),为什么在使用相同的当前轴创建时,我可以在下面添加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

enter image description here

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

1 个答案:

答案 0 :(得分:0)

简短的回答是因为你不能。

稍微长一点的答案是,作为绘图过程的一部分,由于变换堆栈的工作方式,艺术家需要知道它们属于哪个Axes。进入更多细节需要挖掘完整的绘制/转换堆栈。

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

另外,请注意Axis vs Axessee diagram