Python matplotlib 中的文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17086847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Box around text in matplotlib
提问by Marika Blum
how is possible to make a box around text in matplotlib? I have text on three different lines and in three different colors:
如何在 matplotlib 中围绕文本制作一个框?我在三个不同的行上有三种不同颜色的文本:
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.
我看到了教程http://matplotlib.org/users/recipes.html(最后一个例子),但我无法解决问题。提前致谢。
回答by Joe Kington
As the example you linked to mentions, you can use the bboxkwargto add a box.
当你挂提到例如,你可以使用的bboxkwarg添加一个框。
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.)
最后两个是“花式”bbox 补丁,因此填充等以不同的方式设置。(这对于填充之类的简单事情来说相当烦人,尽管它使幕后的实现更简单。)
Also, if you're labeling things in your plot, you'll probably find that annotateis a better choice. Among other things, it allows you to place your text at an offsent in pointsfrom a particular data position.
此外,如果您在情节中标记事物,您可能会发现这annotate是一个更好的选择。除其他外,它允许您将文本放置在距特定数据位置的点的偏移处。
回答by Rutger Kassies
A solution could be to explore the boundingbox from the text objects and generate a box yourself. Its not very convenient. Perhaps my example can be improved, transformations always confuse me a bit.
一个解决方案可能是从文本对象中探索边界框并自己生成一个框。它不是很方便。也许我的例子可以改进,转换总是让我有点困惑。
import matplotlib.patches as patches
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1,1)
t1 = axs.text(0.4,0.6, 'Hello world line 1', ha='center', color='red', weight='bold', transform=axs.transAxes)
t2 = axs.text(0.5,0.5, 'Hello world line 2', ha='center', color='green', weight='bold', transform=axs.transAxes)
t3 = axs.text(0.6,0.4, 'Hello world line 3', ha='center', color='blue', weight='bold', transform=axs.transAxes)
fig.canvas.draw()
textobjs = [t1,t2,t3]
xmin = min([t.get_window_extent().xmin for t in textobjs])
xmax = max([t.get_window_extent().xmax for t in textobjs])
ymin = min([t.get_window_extent().ymin for t in textobjs])
ymax = max([t.get_window_extent().ymax for t in textobjs])
xmin, ymin = fig.transFigure.inverted().transform((xmin, ymin))
xmax, ymax = fig.transFigure.inverted().transform((xmax, ymax))
rect = patches.Rectangle((xmin,ymin),xmax-xmin,ymax-ymin, facecolor='grey', alpha=0.2, transform=fig.transFigure)
axs.add_patch(rect)
You might want to add a small buffer etc, but the idea would stay the same.
您可能想要添加一个小缓冲区等,但这个想法将保持不变。


回答by esmit
There is some documentation online somewhere (the best I can find quickly is http://matplotlib.org/users/annotations_guide.html) for using VPackerand an AnnotationBboxto put together several texts of varying font properties.
某处有一些在线文档(我能快速找到的最好的是http://matplotlib.org/users/annotations_guide.html)用于使用VPacker和AnnotationBbox将几个不同字体属性的文本放在一起。
from matplotlib.offsetbox import TextArea, VPacker, AnnotationBbox
from pylab import *
fig = figure(1)
ax = gca()
texts = ['alpha','beta','epsilon']
colors = ['red','cyan','black']
Texts = []
for t,c in zip(texts,colors):
Texts.append(TextArea(t,textprops=dict(color=c)))
texts_vbox = VPacker(children=Texts,pad=0,sep=0)
ann = AnnotationBbox(texts_vbox,(.02,.5),xycoords=ax.transAxes,
box_alignment=(0,.5),bboxprops =
dict(facecolor='wheat',boxstyle='round',color='black'))
ann.set_figure(fig)
fig.artists.append(ann)
I'm not sure why both of the last two lines are needed. I would think the second to last would suffice.
我不确定为什么最后两行都需要。我认为倒数第二个就足够了。

