sidebarDepth: 3

sidebar: auto

Text properties and layout

Controlling properties of text and its layout with Matplotlib.

The matplotlib.text.Text instances have a variety of properties which can be configured via keyword arguments to the text commands (e.g., title(), xlabel() and text()).


Property Value Type

alpha float

backgroundcolor any matplotlib color

bbox Rectangle prop dict plus key ‘pad’ which is a pad in points

clip_box a matplotlib.transform.Bbox instance

clip_on bool

clip_path a Path instance and a Transform instance, a Patch

color any matplotlib color

family [ ‘serif’ | ‘sans-serif’ | ‘cursive’ | ‘fantasy’ | ‘monospace’ ]

fontproperties a FontProperties instance

horizontalalignment or ha [ ‘center’ | ‘right’ | ‘left’ ]

label any string

linespacing float

multialignment [‘left’ | ‘right’ | ‘center’ ]

name or fontname string e.g., [‘Sans’ | ‘Courier’ | ‘Helvetica’ …]

picker [None|float|boolean|callable]

position (x, y)

rotation [ angle in degrees | ‘vertical’ | ‘horizontal’ ]

size or fontsize [ size in points | relative size, e.g., ‘smaller’, ‘x-large’ ]

style or fontstyle [ ‘normal’ | ‘italic’ | ‘oblique’ ]

text string or anything printable with ‘%s’ conversion

transform a Transform instance

variant [ ‘normal’ | ‘small-caps’ ]

verticalalignment or va [ ‘center’ | ‘top’ | ‘bottom’ | ‘baseline’ ]

visible bool

weight or fontweight [ ‘normal’ | ‘bold’ | ‘heavy’ | ‘light’ | ‘ultrabold’ | ‘ultralight’]

x float

y float

zorder any number

You can lay out text with the alignment arguments horizontalalignment, verticalalignment, and multialignment. horizontalalignment controls whether the x positional argument for the text indicates the left, center or right side of the text bounding box. verticalalignment controls whether the y positional argument for the text indicates the bottom, center or top side of the text bounding box. multialignment, for newline separated strings only, controls whether the different lines are left, center or right justified. Here is an example which uses the text() command to show the various alignment possibilities. The use of transform=ax.transAxes throughout the code indicates that the coordinates are given relative to the axes bounding box, with 0,0 being the lower left of the axes and 1,1 the upper right.

  1. import matplotlib.pyplot as plt
  2. import matplotlib.patches as patches
  3. # build a rectangle in axes coords
  4. left, width = .25, .5
  5. bottom, height = .25, .5
  6. right = left + width
  7. top = bottom + height
  8. fig = plt.figure()
  9. ax = fig.add_axes([0, 0, 1, 1])
  10. # axes coordinates are 0,0 is bottom left and 1,1 is upper right
  11. p = patches.Rectangle(
  12. (left, bottom), width, height,
  13. fill=False, transform=ax.transAxes, clip_on=False
  14. )
  15. ax.add_patch(p)
  16. ax.text(left, bottom, 'left top',
  17. horizontalalignment='left',
  18. verticalalignment='top',
  19. transform=ax.transAxes)
  20. ax.text(left, bottom, 'left bottom',
  21. horizontalalignment='left',
  22. verticalalignment='bottom',
  23. transform=ax.transAxes)
  24. ax.text(right, top, 'right bottom',
  25. horizontalalignment='right',
  26. verticalalignment='bottom',
  27. transform=ax.transAxes)
  28. ax.text(right, top, 'right top',
  29. horizontalalignment='right',
  30. verticalalignment='top',
  31. transform=ax.transAxes)
  32. ax.text(right, bottom, 'center top',
  33. horizontalalignment='center',
  34. verticalalignment='top',
  35. transform=ax.transAxes)
  36. ax.text(left, 0.5*(bottom+top), 'right center',
  37. horizontalalignment='right',
  38. verticalalignment='center',
  39. rotation='vertical',
  40. transform=ax.transAxes)
  41. ax.text(left, 0.5*(bottom+top), 'left center',
  42. horizontalalignment='left',
  43. verticalalignment='center',
  44. rotation='vertical',
  45. transform=ax.transAxes)
  46. ax.text(0.5*(left+right), 0.5*(bottom+top), 'middle',
  47. horizontalalignment='center',
  48. verticalalignment='center',
  49. fontsize=20, color='red',
  50. transform=ax.transAxes)
  51. ax.text(right, 0.5*(bottom+top), 'centered',
  52. horizontalalignment='center',
  53. verticalalignment='center',
  54. rotation='vertical',
  55. transform=ax.transAxes)
  56. ax.text(left, top, 'rotated\nwith newlines',
  57. horizontalalignment='center',
  58. verticalalignment='center',
  59. rotation=45,
  60. transform=ax.transAxes)
  61. ax.set_axis_off()
  62. plt.show()

sphx_glr_text_props_001

Default Font

The base default font is controlled by a set of rcParams. To set the font for mathematical expressions, use the rcParams beginning with mathtext (see mathtext).


rcParam usage

‘font.family’ List of either names of font or {‘cursive’, ‘fantasy’, ‘monospace’, ‘sans’, ‘sans serif’, ‘sans-serif’, ‘serif’}.

‘font.style’ The default style, ex ‘normal’, ‘italic’.

‘font.variant’ Default variant, ex ‘normal’, ‘small-caps’ (untested)

‘font.stretch’ Default stretch, ex ‘normal’, ‘condensed’ (incomplete)

‘font.weight’ Default weight. Either string or integer

‘font.size’ Default font size in points. Relative font sizes (‘large’, ‘x-small’) are computed against this size.

The mapping between the family aliases ({'cursive', 'fantasy', 'monospace', 'sans', 'sans serif', 'sans-serif', 'serif'}) and actual font names is controlled by the following rcParams:


family alias rcParam with mappings

‘serif’ ‘font.serif’

‘monospace’ ‘font.monospace’

‘fantasy’ ‘font.fantasy’

‘cursive’ ‘font.cursive’

{‘sans’, ‘sans serif’, ‘sans-serif’} ‘font.sans-serif’

which are lists of font names.

Text with non-latin glyphs

As of v2.0 the default font contains glyphs for many western alphabets, but still does not cover all of the glyphs that may be required by mpl users. For example, DejaVu has no coverage of Chinese, Korean, or Japanese.

To set the default font to be one that supports the code points you need, prepend the font name to 'font.family' or the desired alias lists

  1. matplotlib.rcParams['font.sans-serif'] = ['Source Han Sans TW', 'sans-serif']

or set it in your .matplotlibrc file:

  1. font.sans-serif: Source Han Sans TW, Arial, sans-serif

To control the font used on per-artist basis use the 'name', 'fontname' or 'fontproperties' kwargs documented above.

On linux, fc-list can be a useful tool to discover the font name; for example

  1. $ fc-list :lang=zh family
  2. Noto to Sans Mono CJK TC,Noto Sans Mono CJK TC Bold
  3. Noto Sans CJK TC,Noto Sans CJK TC Medium
  4. Noto Sans CJK TC,Noto Sans CJK TC DemiLight
  5. Noto Sans CJK KR,Noto Sans CJK KR Black
  6. Noto Sans CJK TC,Noto Sans CJK TC Black
  7. Noto Sans Mono CJK TC,Noto Sans Mono CJK TC Regular
  8. Noto Sans CJK SC,Noto Sans CJK SC Light

lists all of the fonts that support Chinese.

Download