Skip to main content
 首页 » 编程设计

python之在轴外绘制带文本的箭头

2024年10月01日6EasonJim

我想绘制一个带有横跨多个轴的文本的箭头。读书http://matplotlib.org/users/annotations_guide.html

我做到了:

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) 

这使得

我从 text docs 找到bbox 是 Rectangle 的字典,它有宽度和高度,但是当我设置

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 控制箭头的大小?有没有办法把它一直延伸到右边?

谢谢! 克里斯

请您参考如下方法:

一个选项是为自定义箭头样式构建我们自己的 width 参数。
为此,我们可以将 BoxStyle.RArrow 子类化,并在新创建的类 MyRArrow 中引入一个 width 参数。
然后箭头的点将偏移此宽度。在这个简单的示例中,width 以像素为单位。 最后,我们可以将这个新类注册为 boxstyle "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 

我们现在可以使用这种新样式并指定 boxstyle="myrarrow,pad=0.2, width=150" 作为 bbox 关键字参数。
然后,在图形坐标而不是轴坐标中指定箭头位置也是有意义的。

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()