从 annotate() 向 text() 传递额外的 kwargs

问题描述 投票:0回答:1

我正在尝试在我的图中添加注释,因为有些注释太长了。

注释是使用 annotate() 进行的:

matplotlib.pyplot.annotate(text, xy, xytext=None, xycoords='data', textcoords=None, arrowprops=None, annotation_clip=None, **kwargs)

文档内容如下:

 **kwargs
 Additional kwargs are passed to Text.

这是否意味着我应该能够通过

wrap=True
(这是
text()
annotate()
参数?如何做到这一点?(我对 kwargs 完全陌生,从未使用过它们)

文档

python matplotlib keyword-argument
1个回答
0
投票

enter image description here

看起来

wrap=1
有效,但文本被包裹在图形的右边框处 - 直接使用
Text
艺术家略有不同,因为文本在轴边框处被 截断

import matplotlib.pyplot as plt
from matplotlib.text import Text

text = '''The Olmec colossal heads are at least 17 monumental stone representations of human heads sculpted from large basalt boulders. The heads date from at least before 900 BC and are a distinctive feature of the Olmec civilization of ancient Mesoamerica. All portray mature men with fleshy cheeks, flat noses and slightly crossed eyes; their physical characteristics correspond to a type that is still common among the inhabitants of Tabasco and Veracruz. The boulders were brought from the Sierra de los Tuxtlas, a mountain range in Veracruz.'''
fig, ax = plt.subplots(edgecolor='k', linewidth=3)

ax.add_artist(Text(0.0, 0.85, text, wrap=1, fontsize='x-small'))
ax.add_artist(Text(0.4, 0.55, text, wrap=1, fontsize='x-small'))

ax.annotate(text, (0.0,0.35), wrap=1, fontsize='x-small')
ax.annotate(text, (0.4,0.05), wrap=1, fontsize='x-small')
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.