2017-03-29 3 views
2

複数の軸にまたがって伸びるテキストの矢印を描きたい。軸の外にテキストで矢印を描く

fig,ax = plt.subplots(2,3) 
bbox_props = dict(boxstyle="rarrow,pad=0.2", fc="cyan", ec="b", lw=1) 
t = ax[0,1].text(-0.8, 1.2, "Mag", ha="center", va="center", rotation=0, size=15, bbox=bbox_props) 

arrow

私は幅と高さを有し、BBOXはRectangleの辞書であることtext docsから見つかったが、私を行いいる:http://matplotlib.org/users/annotations_guide.html

を読んで、私はこれまでということですセット

bbox_props = dict(boxstyle="rarrow,pad=0.2", fc="cyan", ec="b", lw=1, width=3) 
t = ax[0,1].text(-0.8, 1.2, "Mag", ha="center", va="center", rotation=0, size=15, bbox=bbox_props) 

は私が衝突を得る:

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-255-006733283545> in <module>() 
    13 # properties of a rectangle : http://matplotlib.org/api/patches_api.html#matplotlib.patches.Rectangle 
    14 
---> 15 t = ax[0,1].text(-0.8, 1.2, "Mag", ha="center", va="center", rotation=0, size=15, bbox=bbox_props) 
    16 plt.savefig('example.png', bbox_inches='tight') 

/Users/chris/anaconda3/lib/python3.5/site-packages/matplotlib/axes/_axes.py in text(self, x, y, s, fontdict, withdash, **kwargs) 
    626   if fontdict is not None: 
    627    t.update(fontdict) 
--> 628   t.update(kwargs) 
    629 
    630   t.set_clip_path(self.patch) 

/Users/chris/anaconda3/lib/python3.5/site-packages/matplotlib/text.py in update(self, kwargs) 
    242   super(Text, self).update(kwargs) 
    243   if bbox: 
--> 244    self.set_bbox(bbox) # depends on font properties 
    245 
    246  def __getstate__(self): 

/Users/chris/anaconda3/lib/python3.5/site-packages/matplotlib/text.py in set_bbox(self, rectprops) 
    514          bbox_transmuter=bbox_transmuter, 
    515          transform=mtransforms.IdentityTransform(), 
--> 516          **props) 
    517   else: 
    518    self._bbox_patch = None 

TypeError: __init__() got multiple values for argument 'width' 

は、どのように私はBBOXと私の矢印の大きさを制御することができますか?それをすべて右に伸ばす方法はありますか?

ありがとうございました! クリス

答えて

1

オプションはに構築された私たち自身のwidth引数になりますがカスタムarrowstyle。
BoxStyle.RArrowをサブクラス化し、新たに作成されたクラスMyRArrowwidth引数を導入することがあります。
矢印のポイントは、この幅だけオフセットされます。この単純な例では、widthはピクセル単位です。 最後に、この新しいクラスをボックススタイル"myrarrow"として登録することがあります。

from matplotlib.patches import BoxStyle 


class MyRArrow(BoxStyle.RArrow): 
     def __init__(self, pad=0.3, width=220): 
      self.width_ = width 
      super(MyRArrow, self).__init__(pad) 

     def transmute(self, x0, y0, width, height, mutation_size): 
      p = BoxStyle.RArrow.transmute(self, x0, y0, 
              width, height, mutation_size) 
      x = p.vertices[:, 0] 
      p.vertices[1:3, 0] = x[1:3] - self.width_ 
      p.vertices[0, 0] = x[0] + self.width_ 
      p.vertices[3:, 0] = x[3:] + self.width_ 
      return p 

BoxStyle._style_list["myrarrow"] = MyRArrow 

我々は今、この新しいスタイルを使用し、BBOXキーワード引数としてboxstyle="myrarrow,pad=0.2, width=150"を指定することができます。
また、軸座標の代わりに図形座標の矢印位置を指定することも意味があります。

import matplotlib.pyplot as plt 

fig,ax = plt.subplots(2,3) 
bbox_props = dict(boxstyle="myrarrow,pad=0.2, width=150", fc="cyan", ec="b", lw=1) 
t = ax[0,1].text(0.5, 0.95, "Mag", ha="center", va="center", 
       rotation=0, size=15, bbox=bbox_props, transform=fig.transFigure) 

plt.show() 

enter image description here

+1

ありがとう、テキストが矢印の中央にとどまるので、これは非常にきちんとした解決策であり、幅を簡単に制御できます。思ったより複雑ですが、私は矢のスタイルについて新しいことを学びます。それは素晴らしいことです! – scichris

1

あなたが両側にスペースの束を追加する場合、それは動作します:

fig,axes = plt.subplots(2,3) 
ax = axes[0,0] 
bbox_props = dict(boxstyle="rarrow, pad=0.2", fc="cyan", ec="b", lw=1) 
t = ax.text(0.15,1.15,45*' '+'Mag'+45*' ', ha="left", va="center", 
      size=15,bbox=bbox_props) 
plt.show() 

結果の画像:

enter image description here

+0

ありがとうございます!これは非常に簡単な解決法であり、それも機能します。私はシンプルさのためにそれが本当に好きです - それは仕事をするハックです。 @重要な答えは非常に詳細ですが、より複雑です。私は短いので、あなたの答えを使用し、非常に便利なコードの私のリポジトリに他の1つを保つでしょう。 – scichris

+0

乾杯、メイト:-)私たちの回答の1つをチェックマークで受け入れて、人々がこれに答えていることを知ってください。 – bernie

関連する問題