无法使用“绘制边界框”;宽度“;matplotlib';什么阴谋?
我尝试在文本上绘制边界框,例如:无法使用“绘制边界框”;宽度“;matplotlib';什么阴谋?,matplotlib,matplotlib-widget,matplotlib.mlab,Matplotlib,Matplotlib Widget,Matplotlib.mlab,我尝试在文本上绘制边界框,例如: 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
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()
在docsmatplotlib.pyplot.text
中,返回一个文本实例,该文本可以采用kwarg“”。这个bbox参数接受一个dict()类型的对象,该对象的属性为。FancyBoxPatch有一个位置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'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在里面
32 props2=FancyBoxPatch((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垂直对齐class='top',bbox=props2
35 )
36
包装中的~\AppData\Local\Continuum\anaconda3\envs\automation\u work\lib\site packages\matplotlib\cbook\deprecation.py(*args,**kwargs)
367 f“%(删除)s。如果{name!r}后面有任何参数,则它们”
368 f“应作为关键字传递,而不是按位置传递。”)
-->369返回函数(*args,**kwargs)
370
371返回包装器
~\AppData\Local\Continuum\anaconda3\envs\automation\u work\lib\site packages\matplotlib\axes\u axes.py文本格式(self、x、y、s、fontdict、withdash、**kwargs)
781其他:
782 t=mtext.Text(x,y,Text=s)
-->783 t.更新(有效)
784
785 t.set\u clip\u路径(self.patch)
更新中的~\AppData\Local\Continuum\anaconda3\envs\automation\u work\lib\site packages\matplotlib\text.py(self,kwargs)
177 super().更新(kwargs)
178如果bbox不是哨兵:
-->179自置箱(bbox)
180
181定义获取状态(自身):
集合中的~\AppData\Local\Continuum\anaconda3\envs\automation\u work\lib\site packages\matplotlib\text.py(self,rectprops)
443
444如果rectprops不是无:
-->445 props=rectprops.copy()
446 boxstyle=props.pop(“boxstyle”,无)
447 pad=道具弹出(“pad”,无)
AttributeError:“FancyBoxPatch”对象没有属性“copy”
这个错误对我来说毫无意义。我觉得这是FancyBoxPatch实现中的一个bug?
有人能帮忙找到问题吗 以下内容将在轴的左上角彼此下方添加两个文本。框的宽度为200像素,与文本大小无关
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredOffsetbox, HPacker, VPacker, TextArea, PaddedBox
fig, ax = plt.subplots()
text1 = TextArea("Short\nText")
of1 = HPacker(width=200, height=None, pad=2, sep=5, align="center", mode="expand", children=[text1])
pb1 = PaddedBox(of1, pad=4, draw_frame=True)
text2 = TextArea("Long text in one line")
of2 = HPacker(width=200, height=None, pad=2, sep=5, align="center", mode="expand", children=[text2])
pb2 = PaddedBox(of2, pad=4, draw_frame=True)
oft = VPacker(width=200, height=None, pad=2, sep=5, align="baseline", children=[pb1, pb2])
t = AnchoredOffsetbox("upper left", pad=0.4, frameon=False, child=oft)
ax.add_artist(t)
plt.show()
您正在传递
FancyBoxPatch
本身。但是,bbox
参数需要一个字典作为输入。我尝试传递一个dictionary对象,props=dict(boxstyle='Round,pad=0.2',facecolor='wheat',alpha=0.5)
有效,但是,props=dict(xy=(0,0.),width=1.,height=1.,boxstyle='Round,pad=0.2',facecolor='wheat',alpha=0.5)
无效!它抛出了一个错误TypeError:\uu init\uuuuuuuuuuuuuo()为参数'xy'获取了多个值,如果我使用props=dict((0,0.),1,1.,boxstyle='Round,pad=0.2',facecolor='wheat',alpha=0.5)
那么我得到TypeError:dict最多需要1个参数,得到3个。如何在dict()
或fancyboxpatch()中使用width
。有什么建议吗?哦,你不能。宽度由文本的宽度决定。你不能改变这一点。你到底想实现什么?我想调整框的宽度,以便在绘图上覆盖多个框。你可以用plt.Rectangle()
绘制一个“框”。所以可能会有更多的要求?