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

Skip to content

Commit 6985632

Browse files
committed
Merge pull request #7444 from soupault/example_annot
Enhance `annotation_demoX` examples
1 parent a1b680b commit 6985632

File tree

3 files changed

+247
-268
lines changed

3 files changed

+247
-268
lines changed

examples/pylab_examples/annotation_demo.py

Lines changed: 89 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -39,104 +39,94 @@
3939
import numpy as np
4040

4141

42-
if 1:
43-
# if only one location is given, the text and xypoint being
44-
# annotated are assumed to be the same
45-
fig = plt.figure()
46-
ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1, 5), ylim=(-3, 5))
47-
48-
t = np.arange(0.0, 5.0, 0.01)
49-
s = np.cos(2*np.pi*t)
50-
line, = ax.plot(t, s)
51-
52-
ax.annotate('figure pixels',
53-
xy=(10, 10), xycoords='figure pixels')
54-
55-
ax.annotate('figure points', xy=(80, 80),
56-
xycoords='figure points')
57-
58-
ax.annotate('point offset from data', xy=(2, 1),
59-
xycoords='data',
60-
xytext=(-15, 25), textcoords='offset points',
61-
arrowprops=dict(facecolor='black', shrink=0.05),
62-
horizontalalignment='right', verticalalignment='bottom',
63-
)
64-
65-
ax.annotate('axes fraction', xy=(3, 1), xycoords='data',
66-
xytext=(0.8, 0.95), textcoords='axes fraction',
67-
arrowprops=dict(facecolor='black', shrink=0.05),
68-
horizontalalignment='right', verticalalignment='top',
69-
)
70-
71-
ax.annotate('figure fraction', xy=(.025, .975),
72-
xycoords='figure fraction',
73-
horizontalalignment='left', verticalalignment='top',
74-
fontsize=20)
75-
76-
# use negative points or pixels to specify from right, top -10, 10
77-
# is 10 points to the left of the right side of the axes and 10
78-
# points above the bottom
79-
ax.annotate('pixel offset from axes fraction', xy=(1, 0),
80-
xycoords='axes fraction',
81-
xytext=(-20, 20),
82-
textcoords='offset pixels',
83-
horizontalalignment='right',
84-
verticalalignment='bottom')
85-
86-
87-
if 1:
88-
# you can specify the xypoint and the xytext in different
89-
# positions and coordinate systems, and optionally turn on a
90-
# connecting line and mark the point with a marker. Annotations
91-
# work on polar axes too. In the example below, the xy point is
92-
# in native coordinates (xycoords defaults to 'data'). For a
93-
# polar axes, this is in (theta, radius) space. The text in this
94-
# example is placed in the fractional figure coordinate system.
95-
# Text keyword args like horizontal and vertical alignment are
96-
# respected
97-
fig = plt.figure()
98-
ax = fig.add_subplot(111, projection='polar')
99-
r = np.arange(0, 1, 0.001)
100-
theta = 2*2*np.pi*r
101-
line, = ax.plot(theta, r)
102-
103-
ind = 800
104-
thisr, thistheta = r[ind], theta[ind]
105-
ax.plot([thistheta], [thisr], 'o')
106-
ax.annotate('a polar annotation',
107-
xy=(thistheta, thisr), # theta, radius
108-
xytext=(0.05, 0.05), # fraction, fraction
109-
textcoords='figure fraction',
110-
arrowprops=dict(facecolor='black', shrink=0.05),
111-
horizontalalignment='left',
112-
verticalalignment='bottom',
113-
)
114-
115-
116-
if 1:
117-
# You can also use polar notation on a cartesian axes. Here the
118-
# native coordinate system ('data') is cartesian, so you need to
119-
# specify the xycoords and textcoords as 'polar' if you want to
120-
# use (theta, radius)
121-
122-
el = Ellipse((0, 0), 10, 20, facecolor='r', alpha=0.5)
123-
124-
fig = plt.figure()
125-
ax = fig.add_subplot(111, aspect='equal')
126-
ax.add_artist(el)
127-
el.set_clip_box(ax.bbox)
128-
ax.annotate('the top',
129-
xy=(np.pi/2., 10.), # theta, radius
130-
xytext=(np.pi/3, 20.), # theta, radius
131-
xycoords='polar',
132-
textcoords='polar',
133-
arrowprops=dict(facecolor='black', shrink=0.05),
134-
horizontalalignment='left',
135-
verticalalignment='bottom',
136-
clip_on=True, # clip to the axes bounding box
137-
)
138-
139-
ax.set_xlim(-20, 20)
140-
ax.set_ylim(-20, 20)
42+
# If only one location is given, the text and xypoint being
43+
# annotated are assumed to be the same
44+
fig = plt.figure()
45+
ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1, 5), ylim=(-3, 5))
46+
47+
t = np.arange(0.0, 5.0, 0.01)
48+
s = np.cos(2*np.pi*t)
49+
line, = ax.plot(t, s)
50+
51+
ax.annotate('figure pixels',
52+
xy=(10, 10), xycoords='figure pixels')
53+
54+
ax.annotate('figure points',
55+
xy=(80, 80), xycoords='figure points')
56+
57+
ax.annotate('point offset from data',
58+
xy=(2, 1), xycoords='data',
59+
xytext=(-15, 25), textcoords='offset points',
60+
arrowprops=dict(facecolor='black', shrink=0.05),
61+
horizontalalignment='right', verticalalignment='bottom')
62+
63+
ax.annotate('axes fraction',
64+
xy=(3, 1), xycoords='data',
65+
xytext=(0.8, 0.95), textcoords='axes fraction',
66+
arrowprops=dict(facecolor='black', shrink=0.05),
67+
horizontalalignment='right', verticalalignment='top')
68+
69+
ax.annotate('figure fraction',
70+
xy=(.025, .975), xycoords='figure fraction',
71+
horizontalalignment='left', verticalalignment='top',
72+
fontsize=20)
73+
74+
# use negative points or pixels to specify from right, top -10, 10
75+
# is 10 points to the left of the right side of the axes and 10
76+
# points above the bottom
77+
ax.annotate('pixel offset from axes fraction',
78+
xy=(1, 0), xycoords='axes fraction',
79+
xytext=(-20, 20), textcoords='offset pixels',
80+
horizontalalignment='right',
81+
verticalalignment='bottom')
82+
83+
84+
# You can specify the xypoint and the xytext in different positions and
85+
# coordinate systems, and optionally turn on a connecting line and mark
86+
# the point with a marker. Annotations work on polar axes too.
87+
# In the example below, the xy point is in native coordinates (xycoords
88+
# defaults to 'data'). For a polar axes, this is in (theta, radius) space.
89+
# The text in the example is placed in the fractional figure coordinate system.
90+
# Text keyword args like horizontal and vertical alignment are respected.
91+
fig = plt.figure()
92+
ax = fig.add_subplot(111, projection='polar')
93+
r = np.arange(0, 1, 0.001)
94+
theta = 2*2*np.pi*r
95+
line, = ax.plot(theta, r)
96+
97+
ind = 800
98+
thisr, thistheta = r[ind], theta[ind]
99+
ax.plot([thistheta], [thisr], 'o')
100+
ax.annotate('a polar annotation',
101+
xy=(thistheta, thisr), # theta, radius
102+
xytext=(0.05, 0.05), # fraction, fraction
103+
textcoords='figure fraction',
104+
arrowprops=dict(facecolor='black', shrink=0.05),
105+
horizontalalignment='left',
106+
verticalalignment='bottom')
107+
108+
109+
# You can also use polar notation on a cartesian axes. Here the native
110+
# coordinate system ('data') is cartesian, so you need to specify the
111+
# xycoords and textcoords as 'polar' if you want to use (theta, radius).
112+
113+
el = Ellipse((0, 0), 10, 20, facecolor='r', alpha=0.5)
114+
115+
fig = plt.figure()
116+
ax = fig.add_subplot(111, aspect='equal')
117+
ax.add_artist(el)
118+
el.set_clip_box(ax.bbox)
119+
ax.annotate('the top',
120+
xy=(np.pi/2., 10.), # theta, radius
121+
xytext=(np.pi/3, 20.), # theta, radius
122+
xycoords='polar',
123+
textcoords='polar',
124+
arrowprops=dict(facecolor='black', shrink=0.05),
125+
horizontalalignment='left',
126+
verticalalignment='bottom',
127+
clip_on=True) # clip to the axes bounding box
128+
129+
ax.set_xlim([-20, 20])
130+
ax.set_ylim([-20, 20])
141131

142132
plt.show()

0 commit comments

Comments
 (0)