You already learned how to annotate the plot by adding legends, but sometimes we want more with text. This recipe will explain and demonstrate more features of text manipulation in matplotlib, giving a powerful toolkit for even advanced typesetting needs.
We will not cover LaTeX support in this recipe, as there is a recipe named Rendering text with LaTeX in this chapter.
We start with listing of the most useful set of functions that matplotlib offers. Most of the functions are available via pyplot
module's interface, but we map their origin function here to allow you to explore more if a particular text feature is not covered in this recipe.
Basic text manipulations and their mapping in matplotlib OO API is presented in the following table:
The base class for text storing and drawing inside windows or data coordinates is the matplotlib.text.Text
class. It supports the definition of the location of text objects as well as a range of properties that we can define, to tune how our strings are going to appear on a figure or a window.
The font properties supported by the matplotlib.text.Text
instances are:
Property |
Values |
Description |
---|---|---|
|
|
Specifies the font name or font family. If this is a list, then it is ordered by priority, so the first matched name will be used. |
|
|
Specifies the size in relative or absolute points or specifies the relative size as a size string. |
|
| |
|
| |
|
|
Specifies the font weight or using a specific weight string. Font weight is defined as the thickness of character outline relative to its height. |
|
|
Specifies the stretch of the font. Stretch is defined as horizontal condensation or expansion. This property is not currently implemented. |
|
- |
Defaults to the |
We can also specify the background box that will contain the text, and which can be further specified in color, borders, and transparency.
The basic text color is read from rcParams['text.color']
, if not specified on the current instance, of course.
Specified text can also be aligned according to visual needs. There are the following alignment properties:
horizontalalignment
or ha
: This allows alignment of text horizontally to center
, left
, and right
.verticalalignment
or va
: The allowed values for this are center
, top
, bottom
, and baseline
.multialignment
: This allows alignment of text strings that span multilines. The allowed values are left
, right
, and center
.So far all is good, but we have a hard time visualizing all these variations in the fonts we can create. So, this is going to illustrate what we can do. In the next code, we will perform the following steps:
The following is the code:
importmatplotlib.pyplot as plt frommatplotlib.font_manager import FontProperties # properties: families = ['serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'] sizes = ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'] styles = ['normal', 'italic', 'oblique'] weights = ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black'] variants = ['normal', 'small-caps'] fig = plt.figure(figsize=(9,17)) ax = fig.add_subplot(111) ax.set_xlim(0,9) ax.set_ylim(0,17) # VAR: FAMILY, SIZE y = 0 size = sizes[0] style = styles[0] weight = weights[0] variant = variants[0] forfamily in families: x = 0 y = y + .5 for size in sizes: y = y + .4 sample = family + " " + size ax.text(x, y, sample, family=family, size=size, style=style, weight=weight, variant=variant) # VAR: STYLE, WEIGHT y = 0 family = families[0] size = sizes[4] variant = variants[0] for weight in weights: x = 5 y = y + .5 for style in styles: y = y + .4 sample = weight + " " + style ax.text(x, y, sample, family=family, size=size, style=style, weight=weight, variant=variant) ax.set_axis_off() plt.show()
The code is really straightforward, as we just iterate twice over tuples of properties printing their values.
The only trick employed here is the positioning of text on the figure canvas, as that allows us to have a nice layout of text samples we can easily compare.
Keep in mind that the default font matplotlib will use is dependent on the operating system you are running, so the preceding screenshot might look slightly different. This screenshot was rendered using standard Ubuntu 13.04 installed fonts.