matplotlib 中的文本框 [英] Box around text in 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屋!