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

Skip to content

Commit e7e2ec6

Browse files
committed
reorg text users guide, added annotations unit
svn path=/trunk/matplotlib/; revision=5445
1 parent a3487c5 commit e7e2ec6

10 files changed

Lines changed: 206 additions & 28 deletions

File tree

doc/devel/outline.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ date plots John has author ?
2323
working with data John has author Darren
2424
custom ticking ? no author ?
2525
masked data Eric has author ?
26-
text ? no author ?
2726
patches ? no author ?
2827
legends ? no author ?
2928
animation John has author ?
3029
collections ? no author ?
31-
mathtext Michael in review John
30+
text - mathtext Michael in review John
31+
text - usetex Darren submitted ?
32+
text - annotations John submitted ?
3233
fonts et al Michael ? no author Darren
3334
pyplot tut John submitted Eric
34-
usetex Darren submitted ?
3535
configuration Darren preliminary ?
3636
win32 install Charlie ? no author Darren
3737
os x install Charlie ? no author ?

doc/faq/howto_faq.rst

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,28 +104,28 @@ in dates on the x-xaxis, you get large horizontal gaps on periods when
104104
there is not data. The solution is to pass in some proxy x-data, eg
105105
evenly sampled indicies, and then use a custom formatter to format
106106
these as dates. The example below shows how to use an 'index formatter'
107-
to achieve the desired plot
107+
to achieve the desired plot::
108108

109-
import numpy as np
110-
import matplotlib.pyplot as plt
111-
import matplotlib.mlab as mlab
112-
import matplotlib.ticker as ticker
109+
import numpy as np
110+
import matplotlib.pyplot as plt
111+
import matplotlib.mlab as mlab
112+
import matplotlib.ticker as ticker
113113

114-
r = mlab.csv2rec('../data/aapl.csv')
115-
r.sort()
116-
r = r[-30:] # get the last 30 days
114+
r = mlab.csv2rec('../data/aapl.csv')
115+
r.sort()
116+
r = r[-30:] # get the last 30 days
117117

118-
N = len(r)
119-
ind = np.arange(N) # the evenly spaced plot indices
118+
N = len(r)
119+
ind = np.arange(N) # the evenly spaced plot indices
120120

121-
def format_date(x, pos=None):
122-
thisind = np.clip(int(x+0.5), 0, N-1)
123-
return r.date[thisind].strftime('%Y-%m-%d')
121+
def format_date(x, pos=None):
122+
thisind = np.clip(int(x+0.5), 0, N-1)
123+
return r.date[thisind].strftime('%Y-%m-%d')
124124

125-
fig = plt.figure()
126-
ax = fig.add_subplot(111)
127-
ax.plot(ind, r.adj_close, 'o-')
128-
ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
129-
fig.autofmt_xdate()
125+
fig = plt.figure()
126+
ax = fig.add_subplot(111)
127+
ax.plot(ind, r.adj_close, 'o-')
128+
ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
129+
fig.autofmt_xdate()
130130

131-
plt.show()
131+
plt.show()

doc/users/annotations.rst

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
.. _annotations-tutorial:
2+
3+
Annotating text
4+
===============
5+
6+
The uses of the basic :func:`~matplotlib.pyplot.text` command above
7+
place text at an arbitrary position on the Axes. A common use case of
8+
text is to annotate some feature of the plot, and the
9+
:func:`~matplotlib.Axes.annotate` method provides helper functionality
10+
to make annotations easy. In an annotation, there are two points to
11+
consider: the location being annotated represented by the argument
12+
``xy`` and the location of the text ``xytext``. Both of these
13+
arguments are ``(x,y)`` tuples.
14+
15+
.. literalinclude:: figures/annotation_basic.py
16+
17+
.. image:: figures/annotation_basic.png
18+
:scale: 50
19+
20+
In this example, both the ``xy`` (arrow tip) and ``xytext`` locations
21+
(text location) are in data coordinates. There are a variety of other
22+
coordinate systems one can choose -- you can specify the coordinate
23+
system of ``xy`` and ``xytext`` with one of the following strings for
24+
``xycoords`` and ``textcoords`` (default is 'data')
25+
26+
==================== ====================================================
27+
argument coordinate system
28+
==================== ====================================================
29+
'figure points' points from the lower left corner of the figure
30+
'figure pixels' pixels from the lower left corner of the figure
31+
'figure fraction' 0,0 is lower left of figure and 1,1 is upper, right
32+
'axes points' points from lower left corner of axes
33+
'axes pixels' pixels from lower left corner of axes
34+
'axes fraction' 0,1 is lower left of axes and 1,1 is upper right
35+
'data' use the axes data coordinate system
36+
==================== ====================================================
37+
38+
For example to place the text coordinates in fractional axes
39+
coordinates, one could do::
40+
41+
ax.annotate('local max', xy=(3, 1), xycoords='data',
42+
xytext=(0.8, 0.95), textcoords='axes fraction',
43+
arrowprops=dict(facecolor='black', shrink=0.05),
44+
horizontalalignment='right', verticalalignment='top',
45+
)
46+
47+
For physical coordinate systems (points or pixels) the origin is the
48+
(bottom, left) of the figure or axes. If the value is negative,
49+
however, the origin is from the (right, top) of the figure or axes,
50+
analogous to negative indexing of sequences.
51+
52+
Optionally, you can specify arrow properties which draws an arrow
53+
from the text to the annotated point by giving a dictionary of arrow
54+
properties in the optional keyword argument ``arrowprops``.
55+
56+
57+
==================== ===========================================================================
58+
``arrowprops`` key description
59+
==================== ===========================================================================
60+
width the width of the arrow in points
61+
frac the fraction of the arrow length occupied by the head
62+
headwidth the width of the base of the arrow head in points
63+
shrink move the tip and base some percent away from the annotated point and text
64+
\*\*kwargs any key for :class:`matplotlib.patches.Polygon`, eg ``facecolor``
65+
==================== ===========================================================================
66+
67+
68+
In the example below, the ``xy`` point is in native coordinates
69+
(``xycoords`` defaults to 'data'). For a polar axes, this is in
70+
(theta, radius) space. The text in this example is placed in the
71+
fractional figure coordinate system. :class:`matplotlib.text.Text`
72+
keyword args like ``horizontalalignment``, ``verticalalignment`` and
73+
``fontsize are passed from the `~matplotlib.Axes.annotate` to the
74+
``Text`` instance
75+
76+
.. literalinclude:: figures/annotation_polar.py
77+
78+
.. image:: figures/annotation_polar.png
79+
:scale: 50
80+
81+
See the `annotations demo
82+
<http://matplotlib.sf.net/examples/pylab_examples/annotation_demo.py>`_ for more
83+
examples.

doc/users/event_handling.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.. _event-handling-tutorial:
22

3-
***********************************
4-
Event Handling and Picking Tutorial
5-
***********************************
3+
**************************
4+
Event handling and picking
5+
**************************
66

77
matplotlib works with 5 user interface toolkits (wxpython, tkinter,
88
qt, gtk and fltk) and in order to support features like interactive
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
fig = plt.figure()
5+
ax = fig.add_subplot(111)
6+
7+
t = np.arange(0.0, 5.0, 0.01)
8+
s = np.cos(2*np.pi*t)
9+
line, = ax.plot(t, s, lw=2)
10+
11+
ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
12+
arrowprops=dict(facecolor='black', shrink=0.05),
13+
)
14+
15+
ax.set_ylim(-2,2)
16+
plt.show()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
fig = plt.figure()
5+
ax = fig.add_subplot(111, polar=True)
6+
r = np.arange(0,1,0.001)
7+
theta = 2*2*np.pi*r
8+
line, = ax.plot(theta, r, color='#ee8d18', lw=3)
9+
10+
ind = 800
11+
thisr, thistheta = r[ind], theta[ind]
12+
ax.plot([thistheta], [thisr], 'o')
13+
ax.annotate('a polar annotation',
14+
xy=(thistheta, thisr), # theta, radius
15+
xytext=(0.05, 0.05), # fraction, fraction
16+
textcoords='figure fraction',
17+
arrowprops=dict(facecolor='black', shrink=0.05),
18+
horizontalalignment='left',
19+
verticalalignment='bottom',
20+
)
21+
plt.show()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
ax = plt.subplot(111)
5+
6+
t = np.arange(0.0, 5.0, 0.01)
7+
s = np.cos(2*np.pi*t)
8+
line, = plt.plot(t, s, lw=2)
9+
10+
plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
11+
arrowprops=dict(facecolor='black', shrink=0.05),
12+
)
13+
14+
plt.ylim(-2,2)
15+
plt.show()

doc/users/index.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111

1212
intro.rst
1313
pyplot_tutorial.rst
14-
mathtext.rst
1514
navigation_toolbar.rst
1615
customizing.rst
16+
index_text.rst
1717
artists.rst
1818
event_handling.rst
19-
usetex.rst
19+
20+
2021

2122

doc/users/pyplot_tutorial.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,46 @@ examples of how to control the alignment and orientation of text.
273273

274274

275275

276+
Using mathematical expressions in text
277+
--------------------------------------
276278

279+
matplotlib accepts TeX equation expressions in any text expression.
280+
For example to write the expression :math:`\sigma_i=15` in the title,
281+
you can write a TeX expression surrounded by dollar signs::
282+
283+
plt.title(r'$\sigma_i=15$')
284+
285+
The ``r`` preceeding the title string is important -- it signifies
286+
that the string is a *raw* string and not to treate backslashes and
287+
python escapes. matplotlib has a built-in TeX expression parser and
288+
layout engine, and ships its own math fonts -- for details see
289+
:ref:`mathtext-tutorial`. Thus you can use mathematical text across platforms
290+
without requiring a TeX installation. For those who have LaTeX and
291+
dvipng installed, you can also use LaTeX to format your text and
292+
incorporate the output directly into your display figures or saved
293+
postscript -- see :ref:`usetex-tutorial`.
294+
295+
296+
Annotating text
297+
---------------
298+
299+
The uses of the basic :func:`~matplotlib.pyplot.text` command above
300+
place text at an arbitrary position on the Axes. A common use case of
301+
text is to annotate some feature of the plot, and the
302+
:func:`~matplotlib.pyplot.annotate` method provides helper
303+
functionality to make annotations easy. In an annotation, there are
304+
two points to consider: the location being annotated represented by
305+
the argument ``xy`` and the location of the text ``xytext``. Both of
306+
these arguments are ``(x,y)`` tuples.
307+
308+
.. literalinclude:: figures/pyplot_annotate.py
309+
310+
.. image:: figures/pyplot_annotate.png
311+
:scale: 50
312+
313+
In this basic example, both the ``xy`` (arrow tip) and ``xytext``
314+
locations (text location) are in data coordinates. There are a
315+
variety of other coordinate systems one can choose -- see
316+
:ref:`annotations-tutorial` for details. More examples can be found
317+
in the `annotations demo
318+
<http://matplotlib.sf.net/examples/pylab_examples/annotation_demo.py>`_

doc/users/usetex.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _usetex-tutorial:
22

33
*************************
4-
Text Rendering With LaTeX
4+
Text rendering With LaTeX
55
*************************
66

77
Matplotlib has the option to use LaTeX to manage all text layout. This

0 commit comments

Comments
 (0)