简体   繁体   English

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

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

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

Here is the code from the example : 以下是示例中的代码:

# 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 coordinates Matplotlib坐标

Using transform=ax.transAxes we can put elements inside a plot in using coordinate system in which point (0, 0) is lower left corner, (0, 1) upper left, (1, 1) upper right, and so on. 使用transform=ax.transAxes我们可以使用坐标系将元素放入绘图中,其中点(0,0)是左下角,(0,1)左上角,(1,1)是右上角,依此类推。

To be specific: if we put a text box using position (0, 0) a specific point called anchor will be placed in lower left corner. 具体来说:如果我们使用位置(0,0)放置一个文本框,则会在左下角放置一个名为anchor的特定点。 To change anchor you need to add two arguments to the function call: verticalalignment (possible values: center , top , bottom , baseline ) and horizontalalignment (possible values: center , right , left ). 要更改锚点,您需要为函数调用添加两个参数: verticalalignment (可能的值: centertopbottombaseline )和horizontalalignment (可能的值: centerrightleft )。

So to put box in lower left corner, you need to put lower left corner of the box in the lower left corner of the figure: 因此,要将框放在左下角,您需要将框的左下角放在图的左下角:

# 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)

Anyways here is the link to ipython-notebook with example for all placements . 无论如何,这里是ipython-notebook的链接, 其中包含所有展示位置的示例

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

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