File: text_layout.py

package info (click to toggle)
matplotlib 3.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,264 kB
  • sloc: python: 123,969; cpp: 57,655; ansic: 29,431; objc: 2,244; javascript: 757; makefile: 163; sh: 111
file content (67 lines) | stat: -rw-r--r-- 2,199 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
===========
Text Layout
===========

Create text with different alignment and rotation.
"""

import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig = plt.figure()

left, width = .25, .5
bottom, height = .25, .5
right = left + width
top = bottom + height

# Draw a rectangle in figure coordinates ((0, 0) is bottom left and (1, 1) is
# upper right).
p = patches.Rectangle((left, bottom), width, height, fill=False)
fig.add_artist(p)

# Figure.text (aka. plt.figtext) behaves like Axes.text (aka. plt.text), with
# the sole exception that the coordinates are relative to the figure ((0, 0) is
# bottom left and (1, 1) is upper right).
fig.text(left, bottom, 'left top',
         horizontalalignment='left', verticalalignment='top')
fig.text(left, bottom, 'left bottom',
         horizontalalignment='left', verticalalignment='bottom')
fig.text(right, top, 'right bottom',
         horizontalalignment='right', verticalalignment='bottom')
fig.text(right, top, 'right top',
         horizontalalignment='right', verticalalignment='top')
fig.text(right, bottom, 'center top',
         horizontalalignment='center', verticalalignment='top')
fig.text(left, 0.5*(bottom+top), 'right center',
         horizontalalignment='right', verticalalignment='center',
         rotation='vertical')
fig.text(left, 0.5*(bottom+top), 'left center',
         horizontalalignment='left', verticalalignment='center',
         rotation='vertical')
fig.text(0.5*(left+right), 0.5*(bottom+top), 'middle',
         horizontalalignment='center', verticalalignment='center',
         fontsize=20, color='red')
fig.text(right, 0.5*(bottom+top), 'centered',
         horizontalalignment='center', verticalalignment='center',
         rotation='vertical')
fig.text(left, top, 'rotated\nwith newlines',
         horizontalalignment='center', verticalalignment='center',
         rotation=45)

plt.show()

#############################################################################
#
# ------------
#
# References
# """"""""""
#
# The use of the following functions, methods, classes and modules is shown
# in this example:

import matplotlib
matplotlib.figure.Figure.add_artist
matplotlib.figure.Figure.text