2017-11-14 27 views
1

matplotlibにオブジェクト指向のアプローチを使用すると、ax.annotateを使用するときに描画される矢印にアクセスする方法があります。matplotlibでax.annotateを使用して矢印とテキストを返す

このコマンドは、テキストをオブジェクトとして返しますが、矢印は返しません。また、show_childrenコマンドを使用しているときに矢印を見つけることができません。

この矢印にアクセスできますか?私は単に私のプロット上にすべての矢印を取得し、その色を変更したいです。

plt.plot(np.arange(5), 2* np.arange(5)) 
plt.plot(np.arange(5), 3*np.arange(5)) 
ax = plt.gca() 

text = ax.annotate('TEST', xytext=(2,10), xy=(2,2), arrowprops=dict(arrowstyle="->")) 

ax.get_children() 

戻り

[<matplotlib.lines.Line2D at 0x207dcdba978>, 
<matplotlib.lines.Line2D at 0x207de1e47f0>, 
Text(2,10,'TEST'), 
<matplotlib.spines.Spine at 0x207dcb81518>, 
<matplotlib.spines.Spine at 0x207de05b320>, 
<matplotlib.spines.Spine at 0x207de0b7828>, 
<matplotlib.spines.Spine at 0x207de1d9080>, 
    <matplotlib.axis.XAxis at 0x207de1d9f28>, 
<matplotlib.axis.YAxis at 0x207de049358>, 
Text(0.5,1,''), 
Text(0,1,''), 
Text(1,1,''), 
<matplotlib.patches.Rectangle at 0x207de049d30>] 

矢印の色がarrowpropscolor引数を使用して、注釈の作成時に設定することができることを感謝し

答えて

1

最初のノートを:

ax.annotate('TEST', xytext=(.2,.1), xy=(.2,.2), 
      arrowprops=dict(arrowstyle="->", color="blue")) 

実際に色を変更する必要がある場合rds、あなたはテキストから矢印のパッチを入手する必要があります。
矢印がAnnotationで生成される場合、Annotationオブジェクトの属性はarrow_patchです。

text = ax.annotate(...) 
text.arrow_patch.set_color("red") 

もちろん、すべての子をループして矢印が含まれているかどうかを確認することもできます。完全な例:

import matplotlib.pyplot as plt 
ax = plt.gca() 

text = ax.annotate('TEST', xytext=(.2,.1), xy=(.2,.2), arrowprops=dict(arrowstyle="->")) 
text2 = ax.annotate('TEST2', xytext=(.5,.5), xy=(.2,.2), arrowprops=dict(arrowstyle="->")) 

# access each object individually 
#text.arrow_patch.set_color("red") 
# or 

for child in ax.get_children(): 
    if isinstance(child,type(text)): 
     if hasattr(child, "arrow_patch"): 
      child.arrow_patch.set_color("red") 

plt.show() 
関連する問題