Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 242e35c

Browse files
committed
added text properties to users guide
svn path=/trunk/matplotlib/; revision=5446
1 parent e7e2ec6 commit 242e35c

4 files changed

Lines changed: 144 additions & 42 deletions

File tree

doc/users/figures/pyplot_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
plt.xlabel('Smarts')
1212
plt.ylabel('Probability')
1313
plt.title('Histogram of IQ')
14-
plt.text(85, .025, r'$\mu=100,\ \sigma=15$')
14+
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
1515
plt.axis([40, 160, 0, 0.03])
1616
plt.grid(True)
1717

doc/users/figures/text_layout.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import matplotlib.pyplot as plt
2+
import matplotlib.patches as patches
3+
4+
# build a rectangle in axes coords
5+
left, width = .25, .5
6+
bottom, height = .25, .5
7+
right = left + width
8+
top = bottom + height
9+
10+
fig = plt.figure()
11+
ax = fig.add_subplot(111)
12+
13+
# axes coordinates are 0,0 is bottom left and 1,1 is upper right
14+
p = patches.Rectangle(
15+
(left, bottom), width, height,
16+
fill=False, transform=ax.transAxes, clip_on=False
17+
)
18+
19+
ax.add_patch(p)
20+
21+
ax.text(left, bottom, 'left top',
22+
horizontalalignment='left',
23+
verticalalignment='top',
24+
transform=ax.transAxes)
25+
26+
ax.text(left, bottom, 'left bottom',
27+
horizontalalignment='left',
28+
verticalalignment='bottom',
29+
transform=ax.transAxes)
30+
31+
ax.text(right, top, 'right bottom',
32+
horizontalalignment='right',
33+
verticalalignment='bottom',
34+
transform=ax.transAxes)
35+
36+
ax.text(right, top, 'right top',
37+
horizontalalignment='right',
38+
verticalalignment='top',
39+
transform=ax.transAxes)
40+
41+
ax.text(right, bottom, 'center top',
42+
horizontalalignment='center',
43+
verticalalignment='top',
44+
transform=ax.transAxes)
45+
46+
ax.text(left, 0.5*(bottom+top), 'right center',
47+
horizontalalignment='right',
48+
verticalalignment='center',
49+
rotation='vertical',
50+
transform=ax.transAxes)
51+
52+
ax.text(left, 0.5*(bottom+top), 'left center',
53+
horizontalalignment='left',
54+
verticalalignment='center',
55+
rotation='vertical',
56+
transform=ax.transAxes)
57+
58+
ax.text(0.5*(left+right), 0.5*(bottom+top), 'middle',
59+
horizontalalignment='center',
60+
verticalalignment='center',
61+
fontsize=20, color='red',
62+
transform=ax.transAxes)
63+
64+
ax.text(right, 0.5*(bottom+top), 'centered',
65+
horizontalalignment='center',
66+
verticalalignment='center',
67+
rotation='vertical',
68+
transform=ax.transAxes)
69+
70+
ax.text(left, top, 'rotated\nwith newlines',
71+
horizontalalignment='center',
72+
verticalalignment='center',
73+
rotation=45,
74+
transform=ax.transAxes)
75+
76+
ax.set_axis_off()
77+
plt.show()

doc/users/pyplot_tutorial.rst

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ as argument
153153

154154
.. _multiple-figs-axes:
155155

156-
Working with multiple figure and axes
157-
=====================================
156+
Working with multiple figures and axes
157+
======================================
158158

159159

160160
Matlab, and :mod:`~matplotlib.pyplot`, have the concept of the current
@@ -232,45 +232,7 @@ arguments into the text functions or using
232232

233233
t = plt.xlabel('my data', fontsize=14, color='red')
234234

235-
The following font properties can be set
236-
237-
========================== ==============================================================================
238-
Property Value Type
239-
========================== ==============================================================================
240-
alpha float
241-
backgroundcolor any matplotlib color
242-
bbox rectangle prop dict plus key 'pad' which is a pad in points
243-
clip_box a matplotlib.transform.Bbox instance
244-
clip_on [True | False]
245-
clip_path a Path instance and a Transform instance, a Patch
246-
color any matplotlib color
247-
family [ 'serif' | 'sans-serif' | 'cursive' | 'fantasy' | 'monospace' ]
248-
fontproperties a matplotlib.font_manager.FontProperties instance
249-
horizontalalignment or ha [ 'center' | 'right' | 'left' ]
250-
label any string
251-
linespacing float
252-
multialignment ['left' | 'right' | 'center' ]
253-
name or fontname string eg, ['Sans' | 'Courier' | 'Helvetica' ...]
254-
picker [None|float|boolean|callable]
255-
position (x,y)
256-
rotation [ angle in degrees 'vertical' | 'horizontal'
257-
size or fontsize [ size in points | relative size eg 'smaller', 'x-large' ]
258-
style or fontstyle [ 'normal' | 'italic' | 'oblique']
259-
text string or anything printable with '%s' conversion
260-
transform a matplotlib.transform transformation instance
261-
variant [ 'normal' | 'small-caps' ]
262-
verticalalignment or va [ 'center' | 'top' | 'bottom' | 'baseline' ]
263-
visible [True | False]
264-
weight or fontweight [ 'normal' | 'bold' | 'heavy' | 'light' | 'ultrabold' | 'ultralight']
265-
x float
266-
y float
267-
zorder any number
268-
========================== ==============================================================================
269-
270-
271-
See `align_text <http://matplotlib.sf.net/screenshots.html#align_text>`_ for
272-
examples of how to control the alignment and orientation of text.
273-
235+
These properties are covered in more detail in :ref:`text-properties`.
274236

275237

276238
Using mathematical expressions in text

doc/users/text_props.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.. _text-properties:
2+
3+
Text properties and layout
4+
==========================
5+
6+
The :class:`matplotlib.text.Text` instances have a variety of
7+
properties which can be configured via keyword arguments to the text
8+
commands (eg :func:`~matplotlib.pyplot.title`,
9+
:func:`~matplotlib.pyplot.xlabel` and :func:`~matplotlib.pyplot.text`).
10+
11+
========================== ==============================================================================
12+
Property Value Type
13+
========================== ==============================================================================
14+
alpha float
15+
backgroundcolor any matplotlib color
16+
bbox rectangle prop dict plus key 'pad' which is a pad in points
17+
clip_box a matplotlib.transform.Bbox instance
18+
clip_on [True | False]
19+
clip_path a Path instance and a Transform instance, a Patch
20+
color any matplotlib color
21+
family [ 'serif' | 'sans-serif' | 'cursive' | 'fantasy' | 'monospace' ]
22+
fontproperties a matplotlib.font_manager.FontProperties instance
23+
horizontalalignment or ha [ 'center' | 'right' | 'left' ]
24+
label any string
25+
linespacing float
26+
multialignment ['left' | 'right' | 'center' ]
27+
name or fontname string eg, ['Sans' | 'Courier' | 'Helvetica' ...]
28+
picker [None|float|boolean|callable]
29+
position (x,y)
30+
rotation [ angle in degrees 'vertical' | 'horizontal'
31+
size or fontsize [ size in points | relative size eg 'smaller', 'x-large' ]
32+
style or fontstyle [ 'normal' | 'italic' | 'oblique']
33+
text string or anything printable with '%s' conversion
34+
transform a matplotlib.transform transformation instance
35+
variant [ 'normal' | 'small-caps' ]
36+
verticalalignment or va [ 'center' | 'top' | 'bottom' | 'baseline' ]
37+
visible [True | False]
38+
weight or fontweight [ 'normal' | 'bold' | 'heavy' | 'light' | 'ultrabold' | 'ultralight']
39+
x float
40+
y float
41+
zorder any number
42+
========================== ==============================================================================
43+
44+
45+
You can layout text with the alignment arguments
46+
``horizontalalignment``, ``verticalalignment``, and
47+
``multialignment``. ``horizontalalignment`` controls whether the x
48+
positional argument for the text indicates the left, center or right
49+
side of the text bounding box. ``verticalalignment`` controls whether
50+
the y positional argument for the text indicates the bottom, center or
51+
top side of the text bounding box. ``multialignment``, for newline
52+
separated strings only, controls whether the different lines are left,
53+
center or right justified. Here is an example which uses the
54+
:func:`~matplotlib.pyplot.text` command to show the various alignment
55+
possibilities. The use of ``transform=ax.transAxes`` throughout the
56+
code indicates that the coordinates are given relative to the axes
57+
bounding box, with 0,0 being the lower left of the axes and 1,1 the
58+
upper right.
59+
60+
.. literalinclude:: figures/text_layout.py
61+
62+
.. image:: figures/text_layout.png
63+
:scale: 50

0 commit comments

Comments
 (0)