matplotlib中的文本框 [英] Box around text in matplotlib

查看:170
本文介绍了matplotlib中的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在matplotlib中的文本周围打一个框? 我在三种不同的行上以三种不同的颜色显示文本:

how is possible to make a box around text in matplotlib? I have text on three different lines and in three different colors:

 ax.text(2,1, 'alpha', color='red')
 ax.text(2,2, 'beta', color='cyan')
 ax.text(2,3, 'epsilon', color='black')

我看到了 http://matplotlib.org/users/recipes.html 教程(最后一个示例),但是我无法解决问题. 预先感谢.

I saw the tutorial http://matplotlib.org/users/recipes.html (last example) but I can't solve the problem. Thanks in advance.

推荐答案

作为您链接提到的示例,您可以使用

As the example you linked to mentions, you can use the bbox kwarg to add a box.

我认为您对如何设置盒子的颜色等感到困惑?举个简单的例子:

I assume you're confused on how to set the color, etc, of the box? As a quick example:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

ax.text(0.5, 0.8, 'Test', color='red', 
        bbox=dict(facecolor='none', edgecolor='red'))

ax.text(0.5, 0.6, 'Test', color='blue', 
        bbox=dict(facecolor='none', edgecolor='blue', pad=10.0))

ax.text(0.5, 0.4, 'Test', color='green', 
        bbox=dict(facecolor='none', edgecolor='green', boxstyle='round'))

ax.text(0.5, 0.2, 'Test', color='black', 
        bbox=dict(facecolor='none', edgecolor='black', boxstyle='round,pad=1'))

plt.show()

最后两个是花式" bbox修补程序,因此填充等的设置方式不同. (虽然它使实现更简单,但对于诸如填充之类的简单事情却很烦人.)

The last two are "Fancy" bbox patches, so the padding, etc is set in a different manner. (Which is rather annoying for simple things like padding, though it makes the implementation simpler behind-the-scenes.)

此外,如果您在情节中标记东西,您可能会发现

Also, if you're labeling things in your plot, you'll probably find that annotate is a better choice. Among other things, it allows you to place your text at an offsent in points from a particular data position.

这篇关于matplotlib中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆