本文介绍了Matplotlib - 在注释函数中设置箭头和文本之间的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 matplotlib 的 annotate 函数中设置箭头和文本之间的距离(填充)?有时文本最终会离箭头太近,我想将它们分开一点.
How do I set the distance (padding) between the arrow and the text in matplotlib's annotate function? Sometimes the text ends up being too close to the arrow and I would like to move them a little further apart.
基本示例:
import matplotlib.pyplot as plt
plt.annotate('Here it is!',xy=(-1,-1),xytext=(0,0),
arrowprops=dict(arrowstyle='->',lw=1.5))
plt.xlim(-10,10)
plt.ylim(-10,10)
plt.show()
推荐答案
对于精美的箭头,您可以使用 bbox
属性:
For fancy arrows you can play with the bbox
properties:
fig, ax = plt.subplots(1, 3, figsize=(7, 3))
pad_val = [-5, 0, 5]
for a,p in zip(ax, pad_val):
a.annotate('Here it is!\npad={}'.format(p),xy=(-1,-1),xytext=(1,1),
arrowprops=dict(arrowstyle='-|>', fc="k", ec="k", lw=1.5),
bbox=dict(pad=p, facecolor="none", edgecolor="none"))
a.set_xlim(-10,10)
a.set_ylim(-10,10)
这里的缺点是您不能在注解后面添加颜色( facecolor ="none"
是必需的),否则箭头将始终粘在框架的边框上,并且变丑.
Here the drawback is that you can't add a color behind the annotation (facecolor="none"
is mandatory), or the arrow will always stick to the border of the frame and it might be ugly.
HTH
这篇关于Matplotlib - 在注释函数中设置箭头和文本之间的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!