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

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

问题描述

如何在 matplotlib 中围绕文本制作一个框?我在三个不同的行上有三种不同颜色的文本:

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

我看到了教程 是更好的选择.除其他外,它允许您将文本放置在距特定数据位置 in 点 的偏移处.

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

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

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天全站免登陆