Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Box around text in 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') 

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

like image 397
Marika Blum Avatar asked Jun 13 '13 12:06

Marika Blum


People also ask

What is BBOX in Matplotlib?

BboxTransformTo is a transformation that linearly transforms points from the unit bounding box to a given Bbox. In your case, the transform itself is based upon a TransformedBBox which again has a Bbox upon which it is based and a transform - for this nested instance an Affine2D transform.

How do I annotate in Matplotlib?

The annotate() function in pyplot module of matplotlib library is used to annotate the point xy with text s. Parameters: This method accept the following parameters that are described below: s: This parameter is the text of the annotation. xy: This parameter is the point (x, y) to annotate.


2 Answers

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

enter image description here

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.

like image 104
Joe Kington Avatar answered Oct 08 '22 17:10

Joe Kington


enter image description here There is some documentation online somewhere (the best I can find quickly is http://matplotlib.org/users/annotations_guide.html) for using VPacker and an AnnotationBbox to put together several texts of varying font properties.

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.

like image 27
esmit Avatar answered Oct 08 '22 17:10

esmit