Skip to content

Commit eecc3ac

Browse files
committed
fixed update of dash/cap join/style
svn path=/trunk/matplotlib/; revision=2830
1 parent f79bee1 commit eecc3ac

File tree

9 files changed

+340
-177
lines changed

9 files changed

+340
-177
lines changed

API_CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Completely reworked the annotations API because I found the old
2+
API cumbersome. The new design is much more legible and easy to
3+
read. See matplotlib.text.Annotation and
4+
examples/annotation_demo.py
15

26
markeredgecolor and markerfacecolor cannot be configured in
37
matplotlibrc any more. Instead, markers are generally colored

CHANGELOG

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2006-10-17 Closed sf bug 1562496 update line props dash/solid/cap/join
2+
styles - JDH
3+
4+
2006-10-17 Complete overhaul of the annotations API and example code -
5+
See matplotlib.text.Annotation and
6+
examples/annotation_demo.py JDH
7+
18
2006-10-12 Committed Manuel Metz's StarPolygon code and
29
examples/scatter_star_poly.py - JDH
310

boilerplate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def %(func)s(*args, **kwargs):
9393
'legend',
9494
'table',
9595
'text',
96+
'annotate',
9697
)
9798

9899
cmappable = {

examples/annotation_demo.py

Lines changed: 84 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,81 @@
11
"""
2-
Some examples of how to annotate various artists.
3-
4-
5-
See matplotlib.text.Annotation for details
2+
Some examples of how to annotate points in figures. You specify an
3+
annotation point xy=(x,y) and a text point xytext=(x,y) for the
4+
annotated points and text location, respectively. Optionally, you can specify the coordinate system of xy and xytext with one of the following strings for xycoords and textcoords (default is 'data')
5+
6+
7+
'figure points' : points from the lower left corner of the figure
8+
'figure pixels' : pixels from the lower left corner of the figure
9+
'figure fraction' : 0,0 is lower left of figure and 1,1 is upper, right
10+
'axes points' : points from lower left corner of axes
11+
'axes pixels' : pixels from lower left corner of axes
12+
'axes fraction' : 0,1 is lower left of axes and 1,1 is upper right
13+
'data' : use the axes data coordinate system
14+
15+
Optionally, you can specify a line which connects the text to the
16+
annotated point by giving a dictionary of line properties lineprops
17+
(see matplotlib.lines.Line2D for line properties) and a marker
18+
properties markerprops
19+
20+
For physical coordinate systems (points or pixels) the origin is the
21+
(bottom, left) of the figure or axes. If the value is negative,
22+
however, the origin is from the (right, top) of the figure or axes,
23+
analogous to negative indexing of sequences.
624
"""
7-
from pylab import figure, show, nx
8-
from matplotlib.patches import Rectangle, CirclePolygon, Ellipse
9-
from matplotlib.text import Annotation
10-
11-
if 1:
12-
fig = figure()
13-
ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1,5), ylim=(-3,5))
14-
15-
rect = Rectangle((0.5, 0.5), 1, 3, alpha=0.3)
16-
ax.add_patch(rect)
17-
18-
t = nx.arange(0.0, 5.0, 0.01)
19-
s = nx.sin(2*nx.pi*t)
20-
line, = ax.plot(t, s, lw=3, color='purple')
21-
22-
a = Annotation(rect, 'A: rect', loc=('outside right', 'outside top'),
23-
color='blue')
24-
ax.add_artist(a)
2525

26-
b = Annotation(rect, 'B: rect', loc=('inside left', 'inside top'),
27-
autopad=8, color='blue')
28-
ax.add_artist(b)
29-
30-
c = Annotation(rect, 'C: rect', loc=('center', 'center'), color='blue')
31-
ax.add_artist(c)
32-
33-
d = Annotation(ax, 'bottom corner', loc=('inside right', 'inside bottom'),
34-
color='red', autopad=40, lineprops=dict(lw=2, color='red', shrink=4))
35-
ax.add_artist(d)
36-
37-
e = Annotation(ax, 'E: an axes title', loc=('center', 'outside top'),
38-
color='red')
39-
ax.add_artist(e)
40-
41-
f = Annotation(fig, 'F: a figure title', loc=('center', 'inside top'),
42-
autopad=10, size=16, color='green')
43-
ax.add_artist(f)
44-
45-
g = Annotation(line, 'localmax', loc=(2.25, 1), padx=20, pady=80,
46-
color='black', size=18,
47-
lineprops=dict(lw=2, color='k', shrink=5., xalign='center'))
48-
ax.add_artist(g)
49-
50-
fig.savefig('annotation_demo')
5126

27+
from pylab import figure, show, nx
28+
from matplotlib.patches import Ellipse
5229

5330
if 1:
54-
# here are some annotations using various coordinate systems. If you
55-
# pass in loc=(x,y) where x,y are scalars, then you can specify the
56-
# following strings for the coordinate system
57-
# 'figure points' : points from the lower left corner of the figure
58-
# 'figure pixels' : pixels from the lower left corner of the figure
59-
# 'figure fraction' : 0,0 is lower left of figure and 1,1 is upper, right
60-
# 'axes points' : points from lower left corner of axes
61-
# 'axes pixels' : pixels from lower left corner of axes
62-
# 'axes fraction' : 0,1 is lower left of axes and 1,1 is upper right
63-
# 'data' : use the coordinate system of the object being annotated (default)
64-
65-
31+
# if only one location is given, the text and xypoint being
32+
# annotated are assumed to be the same
6633
fig = figure()
6734
ax = fig.add_subplot(111, autoscale_on=False, xlim=(-1,5), ylim=(-3,5))
6835

6936
t = nx.arange(0.0, 5.0, 0.01)
70-
s = nx.sin(2*nx.pi*t)
37+
s = nx.cos(2*nx.pi*t)
7138
line, = ax.plot(t, s, lw=3, color='purple')
7239

73-
a = Annotation(ax, 'A: center', loc=(.5, .5), coords='axes fraction')
74-
ax.add_artist(a)
40+
ax.annotate('axes center', xy=(.5, .5), xycoords='axes fraction',
41+
horizontalalignment='center', verticalalignment='center')
7542

76-
b = Annotation(fig, 'B: pixels', loc=(20, 20), coords='figure pixels')
77-
ax.add_artist(b)
43+
ax.annotate('pixels', xy=(20, 20), xycoords='figure pixels')
7844

79-
c = Annotation(fig, 'C: points', loc=(100, 300), coords='figure points')
80-
ax.add_artist(c)
45+
ax.annotate('points', xy=(100, 300), xycoords='figure points')
8146

82-
d = Annotation(line, 'D: data', loc=(1, 2), coords='data')
83-
ax.add_artist(d)
47+
ax.annotate('local max', xy=(3, 1), xycoords='data',
48+
xytext=(0.9, 0.9), textcoords='axes fraction',
49+
lineprops=dict(lw=2, color='black'),
50+
markerprops=dict(marker='o', markerfacecolor='b'),
51+
horizontalalignment='right', verticalalignment='top',
52+
)
8453

85-
# use positive points or pixels to specify from left, bottom
86-
e = Annotation(fig, 'E: a figure title (fraction)', loc=(.05, .95), coords='figure fraction',
87-
horizontalalignment='left', verticalalignment='top',
88-
fontsize=20)
89-
ax.add_artist(e)
54+
ax.annotate('a fractional title', xy=(.025, .975),
55+
xycoords='figure fraction',
56+
horizontalalignment='left', verticalalignment='top',
57+
fontsize=20)
9058

91-
# use negative points or pixels to specify from right, top
92-
f = Annotation(fig, 'F: a figure title (points)', loc=(-10, -10), coords='figure points',
93-
horizontalalignment='right', verticalalignment='top',
94-
fontsize=20)
95-
ax.add_artist(f)
59+
# use negative points or pixels to specify from right, top -10, 10
60+
# is 10 points to the left of the right side of the axes and 10
61+
# points above the bottom
62+
ax.annotate('bottom right (points)', xy=(-10, 10),
63+
xycoords='axes points',
64+
horizontalalignment='right', verticalalignment='bottom',
65+
fontsize=20)
9666

9767
fig.savefig('annotation_coords')
9868

9969
if 1:
100-
# annotations work on polar axes too. The annotation coords below
101-
# are in polar coordinates, and the pads are in physical display
102-
# cartesian coordinates
70+
# you can specify the xypoint and the xytext in different
71+
# positions and coordinate systems, and optionally turn on a
72+
# connecting line and mark the point with a marker. Annotations
73+
# work on polar axes too. In the example below, the xy point is
74+
# in native coordinates (xycoords defaults to 'data'). For a
75+
# polar axes, this is in (theta, radius) space. The text in this
76+
# example is placed in the fractional figure coordinate system.
77+
# Text keyword args like horizontal and vertical alignment are
78+
# respected
10379
fig = figure()
10480
ax = fig.add_subplot(111, polar=True)
10581
r = nx.arange(0,1,0.001)
@@ -109,31 +85,44 @@
10985
ind = 800
11086
thisr, thistheta = r[ind], theta[ind]
11187
ax.plot([thistheta], [thisr], 'o')
112-
a = Annotation(line, 'a polar annotation', loc=(thistheta, thisr),
113-
padx=60, pady=-30,
114-
lineprops=dict(lw=2, color='k', shrink=5.))
115-
116-
ax.add_artist(a)
117-
fig.savefig('annotation_polar')
88+
ax.annotate('a polar annotation',
89+
xy=(thistheta, thisr), # theta, radius
90+
xytext=(0.05, 0.05), # fraction, fraction
91+
textcoords='figure fraction',
92+
lineprops=dict(lw=2, color='k'),
93+
markerprops=dict(marker='o', markersize=6),
94+
horizontalalignment='left',
95+
verticalalignment='bottom',
96+
)
97+
#fig.savefig('annotation_polar')
11898

11999
if 1:
120-
from matplotlib.patches import Ellipse
100+
# You can also use polar notation on a catesian axes. Here the
101+
# native coordinate system ('data') is cartesian, so you need to
102+
# specify the xycoords and textcoords as 'polar' if you want to
103+
# use (theta, radius)
121104

122105
el = Ellipse((0,0), 10, 20, facecolor='r', alpha=0.5)
123106

124107
fig = figure()
125108
ax = fig.add_subplot(111, aspect='equal')
126109
ax.add_artist(el)
127110
el.set_clip_box(ax.bbox)
128-
ax.plot([0], [10], 'o')
129-
a = Annotation(el, 'the top', loc=(nx.pi/2., 10), coords='polar',
130-
padx=30, pady=30,
131-
lineprops=dict(lw=2, color='k', shrink=4.))
132-
ax.add_artist(a)
133-
fig.savefig('annotation_ellipse')
134-
111+
ax.annotate('the top',
112+
xy=(nx.pi/2., 10.), # theta, radius
113+
xytext=(nx.pi/4, 20.), # theta, radius
114+
xycoords='polar',
115+
textcoords='polar',
116+
lineprops=dict(lw=2, color='k'),
117+
markerprops=dict(marker='o', color='red'),
118+
horizontalalignment='left',
119+
verticalalignment='bottom',
120+
)
121+
135122
ax.set_xlim(-20, 20)
136123
ax.set_ylim(-20, 20)
124+
#fig.savefig('annotation_ellipse')
125+
137126

138127

139128
show()

examples/annotation_demo_pylab.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

lib/matplotlib/axes.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
from matplotlib import rcParams
3838
from patches import Patch, Rectangle, Circle, Polygon, Arrow, Wedge, Shadow, FancyArrow, bbox_artist
3939
import table
40-
from text import Text, TextWithDash, _process_text_args
40+
from text import Text, TextWithDash, Annotation, _process_text_args
4141
from transforms import Bbox, Point, Value, Affine, NonseparableTransformation
4242
from transforms import FuncXY, Func, LOG10, IDENTITY, POLAR
4343
from transforms import get_bbox_transform, unit_bbox, one, origin, zero
4444
from transforms import blend_xy_sep_transform, Interval, identity_transform
45-
from transforms import PBox
45+
from transforms import PBox, identity_transform
4646
from font_manager import FontProperties
4747

4848
from quiver import Quiver, QuiverKey
@@ -1774,6 +1774,22 @@ def text(self, x, y, s, fontdict=None,
17741774
if kwargs.has_key('clip_on'): t.set_clip_box(self.bbox)
17751775
return t
17761776

1777+
def annotate(self, *args, **kwargs):
1778+
"""
1779+
annotate(self, s, xyloc, textloc,
1780+
xycoords='data', textcoords='data',
1781+
lineprops=None,
1782+
markerprops=None
1783+
**props)
1784+
1785+
%s
1786+
"""%Annotation.__doc__
1787+
a = Annotation(*args, **kwargs)
1788+
a.set_transform(identity_transform())
1789+
self._set_artist_props(a)
1790+
self.texts.append(a)
1791+
return a
1792+
17771793

17781794
#### Lines and spans
17791795

lib/matplotlib/lines.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,10 @@ def update_from(self, other):
10411041
self._markeredgecolor = other._markeredgecolor
10421042
self._markeredgewidth = other._markeredgewidth
10431043
self._dashSeq = other._dashSeq
1044+
self._dashcapstyle = other._dashcapstyle
1045+
self._dashjoinstyle = other._dashjoinstyle
1046+
self._solidcapstyle = other._solidcapstyle
1047+
self._solidjoinstyle = other._solidjoinstyle
10441048

10451049
self._linestyle = other._linestyle
10461050
self._marker = other._marker

0 commit comments

Comments
 (0)