2016-03-24 9 views

答えて

4

私はビルトインこれを実行するために、任意のはありだとは思わないが、あなたはおそらく、図中の他の軸は、当該軸と同一のバウンディングボックスを持っているかどうかをチェックすることができます。

def has_twin(ax): 
    for other_ax in ax.figure.axes: 
     if other_ax is ax: 
      continue 
     if other_ax.bbox.bounds == ax.bbox.bounds: 
      return True 
    return False 

# Usage: 

fig, ax = plt.subplots() 
print(has_twin(ax)) # False 

ax2 = ax.twinx() 
print(has_twin(ax)) # True 
+0

これはすべきです! –

+0

twins_of_ax = [a!= axとa.bbox.bounds == ax.bbox.boundsの場合、fig.axesのaに対してaはa] – theCake

0

軸に共有軸が​​あるかどうかを確認することができます。しかし、必ずしも双子であるとは限りません。しかし、位置を照会するだけで十分です。

import matplotlib.pyplot as plt 

fig, axes = plt.subplots(2,2) 
ax5 = axes[0,1].twinx() 

def has_twinx(ax): 
    s = ax.get_shared_x_axes().get_siblings(ax) 
    if len(s) > 1: 
     for ax1 in [ax1 for ax1 in s if ax1 is not ax]: 
      if ax1.bbox.bounds == ax.bbox.bounds: 
       return True 
    return False 

print has_twinx(axes[0,1]) 
print has_twinx(axes[0,0]) 
関連する問題