繁体   English   中英

如何将文本放在matplotlib中的绘图框中

[英]How to put text inside a box on a plot in matplotlib

我想在matplotlib图上的一个方框中放一个文本,但是文档只提供了如何将它放在右上角的示例(并且选择不同的角落并不是很简单)。

以下是示例中的代码:

# these are matplotlib.patch.Patch properties
props = dict(boxstyle='round', 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)

Matplotlib坐标

使用transform=ax.transAxes我们可以使用坐标系将元素放入绘图中,其中点(0,0)是左下角,(0,1)左上角,(1,1)是右上角,依此类推。

具体来说:如果我们使用位置(0,0)放置一个文本框,则会在左下角放置一个名为anchor的特定点。 要更改锚点,您需要为函数调用添加两个参数: verticalalignment (可能的值: centertopbottombaseline )和horizontalalignment (可能的值: centerrightleft )。

因此,要将框放在左下角,您需要将框的左下角放在图的左下角:

# place a text box in lower left in axes coords
ax.text(0.05, 0.05, textstr, transform=ax.transAxes, fontsize=14,
    verticalalignment='bottom', bbox=props)

无论如何,这里是ipython-notebook的链接, 其中包含所有展示位置的示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM