Skip to content

Commit c6f65d2

Browse files
committed
minor modernization
svn path=/trunk/matplotlib/; revision=3130
1 parent 214a9ea commit c6f65d2

File tree

8 files changed

+66
-87
lines changed

8 files changed

+66
-87
lines changed

CHANGELOG

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
2007-03-26 Removed colorbar_classic from figure.py; fixed bug in
22
Figure.clf() in which _axobservers was not getting
3-
cleared. - EF
3+
cleared. Modernization and cleanups. - EF
44

55
2007-03-26 Refactored some of the units support -- units now live in
66
the respective x and y Axis instances. See also

CODING_GUIDE

+8-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ and standards. Please edit and extend this document.
55

66
== svn checkouts ==
77

8-
svn co https://svn.sourceforge.net/svnroot/matplotlib/trunk/matplotlib matplotlib
8+
svn co https://svn.sourceforge.net/svnroot/matplotlib/trunk/matplotlib matplotlib
99

1010
== Committing changes ==
1111

@@ -102,36 +102,30 @@ they just get passed through the API to the artist constructor which
102102
looks for suitably named methods and calls them with the value.
103103

104104
As a general rule, the use of **kwargs should be reserved for
105-
pass-through keyword arguments, as in the exmaple above. If I intend
105+
pass-through keyword arguments, as in the examaple above. If I intend
106106
for all the keyword args to be used in some function and not passed
107107
on, I just use the key/value keyword args in the function definition
108108
rather than the **kwargs idiom.
109109

110110
In some cases I want to consume some keys and pass through the others,
111111
in which case I pop the ones I want to use locally and pass on the
112112
rest, eg I pop scalex and scaley in Axes.plot and assume the rest are
113-
Line2D keyword arguments. Whenever you mutate a kwargs dictionary (eg
114-
by popping it), you must first copy it since the user may be explitly
115-
passing in a dictionary which is used across many function calls. As
116-
an example of a copy, pop, passthrough usage, see Axes.plot:
113+
Line2D keyword arguments. As an example of a pop, passthrough
114+
usage, see Axes.plot:
117115

118116
# in axes.py
119117
def plot(self, *args, **kwargs):
120-
kwargs = kwargs.copy()
121-
scalex = popd(kwargs, 'scalex', True)
122-
scaley = popd(kwargs, 'scaley', True)
123-
# Now this would be: scaley = kwargs.pop('scaley', True)
118+
scalex = kwargs.pop('scalex', True)
119+
scaley = kwargs.pop('scaley', True)
124120
if not self._hold: self.cla()
125121
lines = []
126122
for line in self._get_lines(*args, **kwargs):
127123
self.add_line(line)
128124
lines.append(line)
129125

130-
popd is a matplotlib.cbook function to pop an item from a dictionary
131-
with a default value if the item doesn't exist. It is rendered
126+
The matplotlib.cbook function popd() is rendered
132127
obsolete by the pop() dictionary method introduced in Python 2.3,
133-
so it should not be used for new code. See comment added to plot
134-
method above.
128+
so it should not be used for new code.
135129

136130
Note there is a use case when kwargs are meant to be used locally in
137131
the function (not passed on), but you still need the **kwargs idiom.

boilerplate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
def %(func)s(*args, **kwargs):
1616
# allow callers to override the hold state by passing hold=True|False
1717
b = ishold()
18-
h = popd(kwargs, 'hold', None)
18+
h = kwargs.pop('hold', None)
1919
if h is not None:
2020
hold(h)
2121
try:

lib/matplotlib/axes.py

+40-54
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from artist import Artist, setp
1717
from axis import XAxis, YAxis
1818
from cbook import iterable, is_string_like, flatten, enumerate, \
19-
allequal, dict_delall, popd, popall, silent_list, is_numlike, dedent
19+
allequal, dict_delall, popall, silent_list, is_numlike, dedent
2020
from collections import RegularPolyCollection, PolyCollection, LineCollection, \
2121
QuadMesh, StarPolygonCollection, BrokenBarHCollection
2222
from colors import colorConverter, Normalize, Colormap, \
@@ -203,16 +203,15 @@ def _get_next_cycle_color(self):
203203
return color
204204

205205
def __call__(self, *args, **kwargs):
206-
kwargs = kwargs.copy()
207206

208207
if self.axes.xaxis is not None and self.axes.xaxis is not None:
209-
xunits = popd(kwargs, 'xunits', self.axes.xaxis.units)
210-
yunits = popd(kwargs, 'yunits', self.axes.yaxis.units)
208+
xunits = kwargs.pop('xunits', self.axes.xaxis.units)
209+
yunits = kwargs.pop('yunits', self.axes.yaxis.units)
211210
if xunits!=self.axes.xaxis.units:
212211
self.axes.xaxis.set_units(xunits)
213212
if yunits!=self.axes.yaxis.units:
214213
self.axes.yaxis.set_units(yunits)
215-
214+
216215
ret = self._grab_next_args(*args, **kwargs)
217216
return ret
218217

@@ -238,7 +237,7 @@ def _xy_from_y(self, y):
238237
if self.axes.yaxis is not None:
239238
b = self.axes.yaxis.update_units(y)
240239
if b: return arange(len(y)), y, False
241-
240+
242241
y = ma.asarray(y)
243242
if len(y.shape) == 1:
244243
y = y[:,newaxis]
@@ -249,12 +248,12 @@ def _xy_from_y(self, y):
249248
return x,y, True
250249

251250
def _xy_from_xy(self, x, y):
252-
if self.axes.xaxis is not None and self.axes.yaxis is not None:
251+
if self.axes.xaxis is not None and self.axes.yaxis is not None:
253252
bx = self.axes.xaxis.update_units(x)
254253
by = self.axes.yaxis.update_units(y)
255254
# right now multicol is not supported if either x or y are
256255
# unit enabled but this can be fixed..
257-
if bx or by: return x, y, False
256+
if bx or by: return x, y, False
258257

259258
x = ma.asarray(x)
260259
y = ma.asarray(y)
@@ -366,7 +365,6 @@ def makefill(x, y):
366365
return ret
367366

368367
def _plot_3_args(self, tup3, **kwargs):
369-
kwargs = kwargs.copy()
370368
ret = []
371369

372370
x, y, fmt = tup3
@@ -1046,7 +1044,7 @@ def add_collection(self, collection, autolim=False):
10461044
if autolim:
10471045
self.update_datalim(collection.get_verts(self.transData))
10481046

1049-
1047+
10501048
def add_line(self, line):
10511049
'Add a line to the list of plot lines'
10521050
self._set_artist_props(line)
@@ -1068,7 +1066,7 @@ def _update_line_limits(self, line):
10681066
ydata = array([y for x,y in xys])
10691067

10701068
self.update_datalim_numerix( xdata, ydata )
1071-
1069+
10721070

10731071
def add_patch(self, p):
10741072
"""
@@ -1081,7 +1079,7 @@ def add_patch(self, p):
10811079
p.set_clip_box(self.bbox)
10821080
self._update_patch_limits(p)
10831081
self.patches.append(p)
1084-
1082+
10851083
def _update_patch_limits(self, p):
10861084
'update the datalimits for patch p'
10871085
xys = self._get_verts_in_data_coords(
@@ -1102,7 +1100,7 @@ def relim(self):
11021100

11031101
for p in self.patches:
11041102
self._update_patch_limits(patch)
1105-
1103+
11061104
def update_datalim(self, xys):
11071105
'Update the data lim bbox with seq of xy tups or equiv. 2-D array'
11081106
# if no data is set currently, the bbox will ignore its
@@ -1135,25 +1133,25 @@ def _get_verts_in_data_coords(self, trans, xys):
11351133
def _process_unit_info(self, xdata=None, ydata=None, kwargs=None):
11361134
'look for unit kwargs and update the axis instances as necessary'
11371135

1138-
if self.xaxis is None or self.xaxis is None: return
1136+
if self.xaxis is None or self.xaxis is None: return
11391137

11401138

11411139
if xdata is not None:
11421140
self.xaxis.update_units(xdata)
11431141
#print '_process updated xdata: units=%s, converter=%s'%(self.xaxis.units, self.xaxis.converter)
11441142

11451143
if ydata is not None:
1146-
self.yaxis.update_units(ydata)
1147-
#print '_process updated ydata: units=%s, converter=%s'%(self.yaxis.units, self.yaxis.converter)
1144+
self.yaxis.update_units(ydata)
1145+
#print '_process updated ydata: units=%s, converter=%s'%(self.yaxis.units, self.yaxis.converter)
11481146

11491147
# process kwargs 2nd since these will override default units
11501148
if kwargs is not None:
1151-
xunits = popd(kwargs, 'xunits', self.xaxis.units)
1149+
xunits = kwargs.pop('xunits', self.xaxis.units)
11521150
if xunits!=self.xaxis.units:
11531151
self.xaxis.set_units(xunits)
11541152
#print '_process updated xunits kws: units=%s, converter=%s'%(self.xaxis.units, self.xaxis.converter)
11551153

1156-
yunits = popd(kwargs, 'yunits', self.yaxis.units)
1154+
yunits = kwargs.pop('yunits', self.yaxis.units)
11571155
if yunits!=self.yaxis.units:
11581156
self.yaxis.set_units(yunits)
11591157
#print '_process updated yunits kws: units=%s, converter=%s'%(self.yaxis.units, self.yaxis.converter)
@@ -2241,7 +2239,6 @@ def axhspan(self, ymin, ymax, xmin=0, xmax=1, **kwargs):
22412239
%(Polygon)s
22422240
"""
22432241
# convert y axis units
2244-
kwargs = kwargs.copy()
22452242

22462243
trans = blend_xy_sep_transform( self.transAxes, self.transData )
22472244
verts = (xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)
@@ -2283,7 +2280,6 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs):
22832280
%(Polygon)s
22842281
"""
22852282
# convert x axis units
2286-
kwargs = kwargs.copy()
22872283
trans = blend_xy_sep_transform( self.transData, self.transAxes )
22882284
verts = [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)]
22892285
p = Polygon(verts, **kwargs)
@@ -2507,17 +2503,16 @@ def plot(self, *args, **kwargs):
25072503
information
25082504
"""
25092505

2510-
kwargs = kwargs.copy()
2511-
scalex = popd(kwargs, 'scalex', True)
2512-
scaley = popd(kwargs, 'scaley', True)
2506+
scalex = kwargs.pop('scalex', True)
2507+
scaley = kwargs.pop('scaley', True)
25132508

25142509
if not self._hold: self.cla()
25152510
lines = []
25162511

25172512
for line in self._get_lines(*args, **kwargs):
25182513
self.add_line(line)
25192514
lines.append(line)
2520-
2515+
25212516

25222517
self.autoscale_view(scalex=scalex, scaley=scaley)
25232518
return lines
@@ -2605,12 +2600,11 @@ def loglog(self, *args, **kwargs):
26052600
%(Line2D)s
26062601
"""
26072602
if not self._hold: self.cla()
2608-
kwargs = kwargs.copy()
2609-
dx = {'basex': popd(kwargs,'basex', 10),
2610-
'subsx': popd(kwargs,'subsx', None),
2603+
dx = {'basex': kwargs.pop('basex', 10),
2604+
'subsx': kwargs.pop('subsx', None),
26112605
}
2612-
dy = {'basey': popd(kwargs,'basey', 10),
2613-
'subsy': popd(kwargs,'subsy', None),
2606+
dy = {'basey': kwargs.pop('basey', 10),
2607+
'subsy': kwargs.pop('subsy', None),
26142608
}
26152609

26162610
self.set_xscale('log', **dx)
@@ -2645,9 +2639,8 @@ def semilogx(self, *args, **kwargs):
26452639
%(Line2D)s
26462640
"""
26472641
if not self._hold: self.cla()
2648-
kwargs = kwargs.copy()
2649-
d = {'basex': popd(kwargs, 'basex', 10),
2650-
'subsx': popd(kwargs, 'subsx', None),
2642+
d = {'basex': kwargs.pop('basex', 10),
2643+
'subsx': kwargs.pop('subsx', None),
26512644
}
26522645

26532646
self.set_xscale('log', **d)
@@ -2680,9 +2673,8 @@ def semilogy(self, *args, **kwargs):
26802673
26812674
"""
26822675
if not self._hold: self.cla()
2683-
kwargs = kwargs.copy()
2684-
d = {'basey': popd(kwargs,'basey', 10),
2685-
'subsy': popd(kwargs,'subsy', None),
2676+
d = {'basey': kwargs.pop('basey', 10),
2677+
'subsy': kwargs.pop('subsy', None),
26862678
}
26872679
self.set_yscale('log', **d)
26882680
b = self._hold
@@ -2787,7 +2779,6 @@ def xcorr(self, x, y, normed=False, detrend=detrend_none, usevlines=False,
27872779
a = self.vlines(lags, [0], c, **kwargs)
27882780
b = self.axhline(**kwargs)
27892781
else:
2790-
kwargs = kwargs.copy()
27912782
kwargs.setdefault('marker', 'o')
27922783
kwargs.setdefault('linestyle', 'None')
27932784
a, = self.plot(lags, c, **kwargs)
@@ -2859,7 +2850,6 @@ def legend(self, *args, **kwargs):
28592850
handletextsep = 0.02 # the space between the legend line and legend text
28602851
axespad = 0.02 # the border between the axes and legend edge
28612852
"""
2862-
kwargs = kwargs.copy()
28632853
def get_handles():
28642854
handles = self.lines
28652855
handles.extend(self.patches)
@@ -2876,13 +2866,13 @@ def get_handles():
28762866
if label != '_nolegend_':
28772867
handles.append(line)
28782868
labels.append(label)
2879-
loc = popd(kwargs, 'loc', 1)
2869+
loc = kwargs.pop('loc', 1)
28802870

28812871
elif len(args)==1:
28822872
# LABELS
28832873
labels = args[0]
28842874
handles = [h for h, label in zip(get_handles(), labels)]
2885-
loc = popd(kwargs, 'loc', 1)
2875+
loc = kwargs.pop('loc', 1)
28862876

28872877
elif len(args)==2:
28882878
if is_string_like(args[1]) or isinstance(args[1], int):
@@ -2892,7 +2882,7 @@ def get_handles():
28922882
else:
28932883
# LINES, LABELS
28942884
handles, labels = args
2895-
loc = popd(kwargs, 'loc', 1)
2885+
loc = kwargs.pop('loc', 1)
28962886

28972887
elif len(args)==3:
28982888
# LINES, LABELS, LOC
@@ -3776,7 +3766,6 @@ def scatter(self, x, y, s=20, c='b', marker='o', cmap=None, norm=None,
37763766
'8' : (8,0), # octagon
37773767
}
37783768

3779-
kwargs = kwargs.copy()
37803769
self._process_unit_info(xdata=x, ydata=y, kwargs=kwargs)
37813770

37823771
x, y, s, c = delete_masked_points(x, y, s, c)
@@ -4068,15 +4057,14 @@ def quiver_classic(self, U, V, *args, **kwargs ):
40684057
V = V*(S/Nmax)
40694058
N = N*Nmax
40704059

4071-
kwargs = kwargs.copy()
4072-
alpha = popd(kwargs,'alpha', 1.0)
4073-
width = popd(kwargs,'width', .5)
4074-
norm = popd(kwargs,'norm', None)
4075-
cmap = popd(kwargs,'cmap', None)
4076-
vmin = popd(kwargs,'vmin', None)
4077-
vmax = popd(kwargs,'vmax', None)
4078-
color = popd(kwargs,'color', None)
4079-
shading = popd(kwargs,'shading', 'faceted')
4060+
alpha = kwargs.pop('alpha', 1.0)
4061+
width = kwargs.pop('width', .5)
4062+
norm = kwargs.pop('norm', None)
4063+
cmap = kwargs.pop('cmap', None)
4064+
vmin = kwargs.pop('vmin', None)
4065+
vmax = kwargs.pop('vmax', None)
4066+
color = kwargs.pop('color', None)
4067+
shading = kwargs.pop('shading', 'faceted')
40804068

40814069
if len(kwargs):
40824070
raise TypeError, "quiver() got an unexpected keyword argument '%s'"%kwargs.keys()[0]
@@ -4151,8 +4139,6 @@ def fill(self, *args, **kwargs):
41514139
kwargs control the Polygon properties:
41524140
%(Polygon)s
41534141
"""
4154-
kwargs = kwargs.copy()
4155-
41564142
if not self._hold: self.cla()
41574143

41584144
patches = []
@@ -4974,7 +4960,7 @@ def spy(self, Z, precision=None, marker=None, markersize=None,
49744960
self.set_aspect(aspect)
49754961
ret = lines
49764962
self.title.set_y(1.05)
4977-
self.xaxis.tick_top()
4963+
self.xaxis.set_label_position('top')
49784964
self.xaxis.set_major_locator(MaxNLocator(integer=True))
49794965
self.yaxis.set_major_locator(MaxNLocator(integer=True))
49804966
return ret
@@ -5010,7 +4996,7 @@ def matshow(self, Z, **kwargs):
50104996
kw.update(kwargs)
50114997
im = self.imshow(Z, **kw)
50124998
self.title.set_y(1.05)
5013-
self.xaxis.tick_top()
4999+
self.xaxis.set_label_position('top')
50145000
self.xaxis.set_major_locator(MaxNLocator(integer=True))
50155001
self.yaxis.set_major_locator(MaxNLocator(integer=True))
50165002
return im

lib/matplotlib/contour.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import _contour
1919
from cm import ScalarMappable
2020
from cbook import iterable, is_string_like, flatten, enumerate, \
21-
allequal, dict_delall, strip_math, popd, popall, silent_list
21+
allequal, dict_delall, strip_math, popall, silent_list
2222
from colors import colorConverter, Normalize, Colormap, ListedColormap, NoNorm
2323
from collections import PolyCollection, LineCollection
2424
from font_manager import FontProperties

0 commit comments

Comments
 (0)