|
17 | 17 | offset. |
18 | 18 | ''' |
19 | 19 |
|
20 | | -import pylab as P |
| 20 | +import matplotlib.pyplot as plt |
| 21 | +import matplotlib.transforms as mtrans |
| 22 | +import numpy as np |
| 23 | + |
21 | 24 | from matplotlib.transforms import offset_copy |
22 | 25 |
|
23 | | -X = P.arange(7) |
24 | | -Y = X**2 |
| 26 | +xs = np.arange(7) |
| 27 | +ys = xs**2 |
25 | 28 |
|
26 | | -fig = P.figure(figsize=(5, 10)) |
27 | | -ax = P.subplot(2, 1, 1) |
| 29 | +fig = plt.figure(figsize=(5, 10)) |
| 30 | +ax = plt.subplot(2, 1, 1) |
28 | 31 |
|
29 | 32 | # If we want the same offset for each text instance, |
30 | 33 | # we only need to make one transform. To get the |
31 | 34 | # transform argument to offset_copy, we need to make the axes |
32 | 35 | # first; the subplot command above is one way to do this. |
| 36 | +trans_offset = mtrans.offset_copy(ax.transData, fig=fig, |
| 37 | + x=0.05, y=0.10, units='inches') |
33 | 38 |
|
34 | | -transOffset = offset_copy(ax.transData, fig=fig, |
35 | | - x=0.05, y=0.10, units='inches') |
36 | | - |
37 | | -for x, y in zip(X, Y): |
38 | | - P.plot((x,), (y,), 'ro') |
39 | | - P.text(x, y, '%d, %d' % (int(x), int(y)), transform=transOffset) |
| 39 | +for x, y in zip(xs, ys): |
| 40 | + plt.plot((x,), (y,), 'ro') |
| 41 | + plt.text(x, y, '%d, %d' % (int(x), int(y)), transform=trans_offset) |
40 | 42 |
|
41 | 43 |
|
42 | 44 | # offset_copy works for polar plots also. |
| 45 | +ax = plt.subplot(2, 1, 2, polar=True) |
43 | 46 |
|
44 | | -ax = P.subplot(2, 1, 2, polar=True) |
45 | | - |
46 | | -transOffset = offset_copy(ax.transData, fig=fig, y=6, units='dots') |
47 | | - |
48 | | -for x, y in zip(X, Y): |
49 | | - P.polar((x,), (y,), 'ro') |
50 | | - P.text(x, y, '%d, %d' % (int(x), int(y)), |
51 | | - transform=transOffset, |
52 | | - horizontalalignment='center', |
53 | | - verticalalignment='bottom') |
| 47 | +trans_offset = mtrans.offset_copy(ax.transData, fig=fig, y=6, units='dots') |
54 | 48 |
|
| 49 | +for x, y in zip(xs, ys): |
| 50 | + plt.polar((x,), (y,), 'ro') |
| 51 | + plt.text(x, y, '%d, %d' % (int(x), int(y)), |
| 52 | + transform=trans_offset, |
| 53 | + horizontalalignment='center', |
| 54 | + verticalalignment='bottom') |
55 | 55 |
|
56 | | -P.show() |
| 56 | +plt.show() |
0 commit comments