我正在尝试在文本中绘制一个边界框,例如:
from matplotlib.pyplot import text
from matplotlib.patches import FancyBboxPatch
import matplotlib.transforms as mtransforms
import numpy as np
import matplotlib.pyplot as plt
def draw_bbox(ax, bb):
# boxstyle=square with pad=0, i.e. bbox itself.
p_bbox = FancyBboxPatch((bb.xmin, bb.ymin),
abs(bb.width), abs(bb.height),
boxstyle="round,pad=0.1, rounding_size=0.2",
ec="k", fc="none", zorder=10.,facecolor='pink')
ax.add_patch(p_bbox)
np.random.seed(19680801)
fig, ax = plt.subplots()
x = 30*np.random.randn(10000)
mu = x.mean()
median = np.median(x)
sigma = x.std()
textstr = '\n'.join((
r'$\mu=%.2f$' % (mu, ),
r'$\mathrm{median}=%.2f$' % (median, ),
r'$\sigma=%.2f$' % (sigma, )))
ax.hist(x, 50)
# these are matplotlib.patch.Patch properties
props = dict(boxstyle='square, pad=0.2', facecolor='wheat', alpha=0.5 )
# place a text box in upper left in axes coords
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
verticalalignment='top', bbox=props
)
plt.show()
在文档matplotlib.pyplot.text
中返回一个可以采用 kwarg ' bbox ' 的 Text 实例。这个 bbox 参数接受一个 dict() 类型的对象,该对象具有 patch.FancyBboxPatch 的属性。FancyBboxPatch 有一个位置 arg 宽度,如果我想给出,
props2 =FancyBboxPatch(xy=(0., 0.),width=1.,height=1.,boxstyle='Round, pad=0.2', facecolor='pink', alpha=0.5 )
ax.text(0.1, 0.95, textstr, transform=ax.transAxes, fontsize=14,
verticalalignment='top', bbox=props2
)
它给出了以下错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-59-0c8ad0d773a9> in <module>
32 props2 =FancyBboxPatch((0., 0.),1.,1.,boxstyle='Round, pad=0.2', facecolor='pink', alpha=0.5 )
33 ax.text(0.1, 0.95, textstr, transform=ax.transAxes, fontsize=14,
---> 34 verticalalignment='top', bbox=props2
35 )
36
~\AppData\Local\Continuum\anaconda3\envs\automation_work\lib\site-packages\matplotlib\cbook\deprecation.py in wrapper(*args, **kwargs)
367 f"%(removal)s. If any parameter follows {name!r}, they "
368 f"should be pass as keyword, not positionally.")
--> 369 return func(*args, **kwargs)
370
371 return wrapper
~\AppData\Local\Continuum\anaconda3\envs\automation_work\lib\site-packages\matplotlib\axes\_axes.py in text(self, x, y, s, fontdict, withdash, **kwargs)
781 else:
782 t = mtext.Text(x, y, text=s)
--> 783 t.update(effective_kwargs)
784
785 t.set_clip_path(self.patch)
~\AppData\Local\Continuum\anaconda3\envs\automation_work\lib\site-packages\matplotlib\text.py in update(self, kwargs)
177 super().update(kwargs)
178 if bbox is not sentinel:
--> 179 self.set_bbox(bbox)
180
181 def __getstate__(self):
~\AppData\Local\Continuum\anaconda3\envs\automation_work\lib\site-packages\matplotlib\text.py in set_bbox(self, rectprops)
443
444 if rectprops is not None:
--> 445 props = rectprops.copy()
446 boxstyle = props.pop("boxstyle", None)
447 pad = props.pop("pad", None)
AttributeError: 'FancyBboxPatch' object has no attribute 'copy'
这个错误对我没有意义。我觉得这是 FancyBboxPatch 实现中的错误?任何人都可以帮助找到问题吗?