10

単一の注釈テキストを使用して、複数の矢印で複数のデータ ポイントに注釈を付けたいと考えています。私は簡単な回避策を作りました:

ax = plt.gca()
ax.plot([1,2,3,4],[1,4,2,6])
an1 = ax.annotate('Test',
  xy=(2,4), xycoords='data',
  xytext=(30,-80), textcoords='offset points',
  arrowprops=dict(arrowstyle="-|>",
                  connectionstyle="arc3,rad=0.2",
                  fc="w"))
an2 = ax.annotate('Test',
  xy=(3,2), xycoords='data',
  xytext=(0,0), textcoords=an1,
  arrowprops=dict(arrowstyle="-|>",
                  connectionstyle="arc3,rad=0.2",
                  fc="w"))
plt.show()

次の結果を生成します。 ここに画像の説明を入力

しかし、私はこのソリューションがあまり好きではありません。

それに加えて、注釈の外観に影響します (主に半透明の bbox を使用している場合など)。

したがって、誰かが実際の解決策または少なくともそれを実装する方法を知っている場合は、共有してください.

4

2 に答える 2

16

適切な解決策には、あまりにも多くの労力が必要になると思います。_AnnotateBaseをサブクラス化し、複数の矢印のサポートをすべて自分で追加します。しかし、を追加するだけで、視覚的な外観に影響を与える2番目の注釈でその問題を解決することができましたalpha=0.0。したがって、誰もより良いものを提供しない場合は、ここで更新されたソリューション:

def my_annotate(ax, s, xy_arr=[], *args, **kwargs):
  ans = []
  an = ax.annotate(s, xy_arr[0], *args, **kwargs)
  ans.append(an)
  d = {}
  try:
    d['xycoords'] = kwargs['xycoords']
  except KeyError:
    pass
  try:
    d['arrowprops'] = kwargs['arrowprops']
  except KeyError:
    pass
  for xy in xy_arr[1:]:
    an = ax.annotate(s, xy, alpha=0.0, xytext=(0,0), textcoords=an, **d)
    ans.append(an)
  return ans

ax = plt.gca()
ax.plot([1,2,3,4],[1,4,2,6])
my_annotate(ax,
            'Test',
            xy_arr=[(2,4), (3,2), (4,6)], xycoords='data',
            xytext=(30, -80), textcoords='offset points',
            bbox=dict(boxstyle='round,pad=0.2', fc='yellow', alpha=0.3),
            arrowprops=dict(arrowstyle="-|>",
                            connectionstyle="arc3,rad=0.2",
                            fc="w"))
plt.show()

結果の画像: ここに画像の説明を入力してください

于 2013-01-27T09:11:42.850 に答える