2015-03-24 37 views
0

爲什麼我需要爲每個軸添加補丁的新實例?該補丁何時取決於一個座標軸,爲什麼不能將數學定義添加到不同的座標軸上以便多次繪製? 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 

enter image description here

編輯補充:重複的Adding the same Patch instance to multiple subplots in matplotlib但什麼失敗,如何更多的例子。

回答

0

簡短的答案是因爲你不能。

稍長的答案是,作爲繪圖過程的一部分,藝術家需要知道他們屬於什麼Axes由於變換堆棧的工作方式。進入更多細節需要挖掘整個繪製/變換堆棧。

在MPL(2.1〜夏2015年)試圖做到這將引發一個異常(https://github.com/matplotlib/matplotlib/pull/3835

同樣的下一個主版本,小心Axis VS Axessee diagram

+0

嗯,這是pythic 。 (我是不是將'Axes'和'Axis'混淆在代碼對象中,或者只是簡單地使用軸來表示軸心 - 英文 - 複數軸) – cphlewis 2015-03-25 03:22:02

+1

由於在代碼I中命名了Axe和Axis,犧牲英文以明確代碼;) – tacaswell 2015-03-25 03:39:19

相關問題