Skip to content

Commit fcbf883

Browse files
authored
Merge pull request #27286 from anntzer/cleanup
Various cleanups
2 parents ea66786 + df4480c commit fcbf883

File tree

5 files changed

+51
-144
lines changed

5 files changed

+51
-144
lines changed

lib/matplotlib/contour.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,22 +1114,14 @@ def _make_paths_from_contour_generator(self):
11141114
"""Compute ``paths`` using C extension."""
11151115
if self._paths is not None:
11161116
return self._paths
1117-
paths = []
1117+
cg = self._contour_generator
11181118
empty_path = Path(np.empty((0, 2)))
1119-
if self.filled:
1120-
lowers, uppers = self._get_lowers_and_uppers()
1121-
for level, level_upper in zip(lowers, uppers):
1122-
vertices, kinds = \
1123-
self._contour_generator.create_filled_contour(
1124-
level, level_upper)
1125-
paths.append(Path(np.concatenate(vertices), np.concatenate(kinds))
1126-
if len(vertices) else empty_path)
1127-
else:
1128-
for level in self.levels:
1129-
vertices, kinds = self._contour_generator.create_contour(level)
1130-
paths.append(Path(np.concatenate(vertices), np.concatenate(kinds))
1131-
if len(vertices) else empty_path)
1132-
return paths
1119+
vertices_and_codes = (
1120+
map(cg.create_filled_contour, *self._get_lowers_and_uppers())
1121+
if self.filled else
1122+
map(cg.create_contour, self.levels))
1123+
return [Path(np.concatenate(vs), np.concatenate(cs)) if len(vs) else empty_path
1124+
for vs, cs in vertices_and_codes]
11331125

11341126
def _get_lowers_and_uppers(self):
11351127
"""

lib/matplotlib/dviread.py

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -125,64 +125,28 @@ def glyph_name_or_index(self):
125125

126126
# Opcode argument parsing
127127
#
128-
# Each of the following functions takes a Dvi object and delta,
129-
# which is the difference between the opcode and the minimum opcode
130-
# with the same meaning. Dvi opcodes often encode the number of
131-
# argument bytes in this delta.
132-
133-
def _arg_raw(dvi, delta):
134-
"""Return *delta* without reading anything more from the dvi file."""
135-
return delta
136-
137-
138-
def _arg(nbytes, signed, dvi, _):
139-
"""
140-
Read *nbytes* bytes, returning the bytes interpreted as a signed integer
141-
if *signed* is true, unsigned otherwise.
142-
"""
143-
return dvi._arg(nbytes, signed)
144-
145-
146-
def _arg_slen(dvi, delta):
147-
"""
148-
Read *delta* bytes, returning None if *delta* is zero, and the bytes
149-
interpreted as a signed integer otherwise.
150-
"""
151-
if delta == 0:
152-
return None
153-
return dvi._arg(delta, True)
154-
155-
156-
def _arg_slen1(dvi, delta):
157-
"""
158-
Read *delta*+1 bytes, returning the bytes interpreted as signed.
159-
"""
160-
return dvi._arg(delta + 1, True)
161-
162-
163-
def _arg_ulen1(dvi, delta):
164-
"""
165-
Read *delta*+1 bytes, returning the bytes interpreted as unsigned.
166-
"""
167-
return dvi._arg(delta + 1, False)
168-
169-
170-
def _arg_olen1(dvi, delta):
171-
"""
172-
Read *delta*+1 bytes, returning the bytes interpreted as
173-
unsigned integer for 0<=*delta*<3 and signed if *delta*==3.
174-
"""
175-
return dvi._arg(delta + 1, delta == 3)
176-
177-
178-
_arg_mapping = dict(raw=_arg_raw,
179-
u1=partial(_arg, 1, False),
180-
u4=partial(_arg, 4, False),
181-
s4=partial(_arg, 4, True),
182-
slen=_arg_slen,
183-
olen1=_arg_olen1,
184-
slen1=_arg_slen1,
185-
ulen1=_arg_ulen1)
128+
# Each of the following functions takes a Dvi object and delta, which is the
129+
# difference between the opcode and the minimum opcode with the same meaning.
130+
# Dvi opcodes often encode the number of argument bytes in this delta.
131+
_arg_mapping = dict(
132+
# raw: Return delta as is.
133+
raw=lambda dvi, delta: delta,
134+
# u1: Read 1 byte as an unsigned number.
135+
u1=lambda dvi, delta: dvi._arg(1, signed=False),
136+
# u4: Read 4 bytes as an unsigned number.
137+
u4=lambda dvi, delta: dvi._arg(4, signed=False),
138+
# s4: Read 4 bytes as a signed number.
139+
s4=lambda dvi, delta: dvi._arg(4, signed=True),
140+
# slen: Read delta bytes as a signed number, or None if delta is None.
141+
slen=lambda dvi, delta: dvi._arg(delta, signed=True) if delta else None,
142+
# slen1: Read (delta + 1) bytes as a signed number.
143+
slen1=lambda dvi, delta: dvi._arg(delta + 1, signed=True),
144+
# ulen1: Read (delta + 1) bytes as an unsigned number.
145+
ulen1=lambda dvi, delta: dvi._arg(delta + 1, signed=False),
146+
# olen1: Read (delta + 1) bytes as an unsigned number if less than 4 bytes,
147+
# as a signed number if 4 bytes.
148+
olen1=lambda dvi, delta: dvi._arg(delta + 1, signed=(delta == 3)),
149+
)
186150

187151

188152
def _dispatch(table, min, max=None, state=None, args=('raw',)):

lib/mpl_toolkits/axisartist/axislines.py

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,10 @@ def get_axislabel_pos_angle(self, axes):
153153
# TICK
154154

155155
def get_tick_transform(self, axes):
156-
return [axes.get_xaxis_transform(),
157-
axes.get_yaxis_transform()][self.nth_coord]
156+
return [axes.get_xaxis_transform(), axes.get_yaxis_transform()][self.nth_coord]
158157

159158

160159
class _FloatingAxisArtistHelperBase(_AxisArtistHelperBase):
161-
162160
def __init__(self, nth_coord, value):
163161
self.nth_coord = nth_coord
164162
self._value = value
@@ -168,8 +166,7 @@ def get_nth_coord(self):
168166
return self.nth_coord
169167

170168
def get_line(self, axes):
171-
raise RuntimeError(
172-
"get_line method should be defined by the derived class")
169+
raise RuntimeError("get_line method should be defined by the derived class")
173170

174171

175172
class FixedAxisArtistHelperRectilinear(_FixedAxisArtistHelperBase):
@@ -187,10 +184,7 @@ def __init__(self, axes, loc, nth_coord=None):
187184

188185
def get_tick_iterators(self, axes):
189186
"""tick_loc, tick_angle, tick_label"""
190-
if self._loc in ["bottom", "top"]:
191-
angle_normal, angle_tangent = 90, 0
192-
else: # "left", "right"
193-
angle_normal, angle_tangent = 0, 90
187+
angle_normal, angle_tangent = {0: (90, 0), 1: (0, 90)}[self.nth_coord]
194188

195189
major = self.axis.major
196190
major_locs = major.locator()
@@ -207,8 +201,7 @@ def _f(locs, labels):
207201
c = self._to_xy(loc, const=self._pos)
208202
# check if the tick point is inside axes
209203
c2 = tick_to_axes.transform(c)
210-
if mpl.transforms._interval_contains_close(
211-
(0, 1), c2[self.nth_coord]):
204+
if mpl.transforms._interval_contains_close((0, 1), c2[self.nth_coord]):
212205
yield c, angle_normal, angle_tangent, label
213206

214207
return _f(major_locs, major_labels), _f(minor_locs, minor_labels)
@@ -245,20 +238,14 @@ def get_axislabel_pos_angle(self, axes):
245238
data_to_axes = axes.transData - axes.transAxes
246239
p = data_to_axes.transform([self._value, self._value])
247240
verts = self._to_xy(0.5, const=p[fixed_coord])
248-
if 0 <= verts[fixed_coord] <= 1:
249-
return verts, angle
250-
else:
251-
return None, None
241+
return (verts, angle) if 0 <= verts[fixed_coord] <= 1 else (None, None)
252242

253243
def get_tick_transform(self, axes):
254244
return axes.transData
255245

256246
def get_tick_iterators(self, axes):
257247
"""tick_loc, tick_angle, tick_label"""
258-
if self.nth_coord == 0:
259-
angle_normal, angle_tangent = 90, 0
260-
else:
261-
angle_normal, angle_tangent = 0, 90
248+
angle_normal, angle_tangent = {0: (90, 0), 1: (0, 90)}[self.nth_coord]
262249

263250
major = self.axis.major
264251
major_locs = major.locator()
@@ -326,27 +313,18 @@ def __init__(self, axes):
326313

327314
@_api.delete_parameter(
328315
"3.9", "nth_coord", addendum="'nth_coord' is now inferred from 'loc'.")
329-
def new_fixed_axis(self, loc,
330-
nth_coord=None,
331-
axis_direction=None,
332-
offset=None,
333-
axes=None,
334-
):
316+
def new_fixed_axis(
317+
self, loc, nth_coord=None, axis_direction=None, offset=None, axes=None):
335318
if axes is None:
336319
_api.warn_external(
337320
"'new_fixed_axis' explicitly requires the axes keyword.")
338321
axes = self.axes
339322
if axis_direction is None:
340323
axis_direction = loc
341-
helper = FixedAxisArtistHelperRectilinear(axes, loc)
342-
axisline = AxisArtist(axes, helper, offset=offset,
343-
axis_direction=axis_direction)
344-
return axisline
324+
return AxisArtist(axes, FixedAxisArtistHelperRectilinear(axes, loc),
325+
offset=offset, axis_direction=axis_direction)
345326

346-
def new_floating_axis(self, nth_coord, value,
347-
axis_direction="bottom",
348-
axes=None,
349-
):
327+
def new_floating_axis(self, nth_coord, value, axis_direction="bottom", axes=None):
350328
if axes is None:
351329
_api.warn_external(
352330
"'new_floating_axis' explicitly requires the axes keyword.")
@@ -404,8 +382,7 @@ def __call__(self, *args, **kwargs):
404382

405383
def __init__(self, *args, grid_helper=None, **kwargs):
406384
self._axisline_on = True
407-
self._grid_helper = (grid_helper if grid_helper
408-
else GridHelperRectlinear(self))
385+
self._grid_helper = grid_helper if grid_helper else GridHelperRectlinear(self)
409386
super().__init__(*args, **kwargs)
410387
self.toggle_axisline(True)
411388

lib/mpl_toolkits/axisartist/floating_axes.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,8 @@ def get_data_boundary(self, side):
158158
bottom=(lat1, 1),
159159
top=(lat2, 1))[side]
160160

161-
def new_fixed_axis(self, loc,
162-
nth_coord=None,
163-
axis_direction=None,
164-
offset=None,
165-
axes=None):
161+
def new_fixed_axis(
162+
self, loc, nth_coord=None, axis_direction=None, offset=None, axes=None):
166163
if axes is None:
167164
axes = self.axes
168165
if axis_direction is None:
@@ -179,23 +176,16 @@ def new_fixed_axis(self, loc,
179176

180177
# new_floating_axis will inherit the grid_helper's extremes.
181178

182-
# def new_floating_axis(self, nth_coord,
183-
# value,
184-
# axes=None,
185-
# axis_direction="bottom"
186-
# ):
187-
179+
# def new_floating_axis(self, nth_coord, value, axes=None, axis_direction="bottom"):
188180
# axis = super(GridHelperCurveLinear,
189181
# self).new_floating_axis(nth_coord,
190182
# value, axes=axes,
191183
# axis_direction=axis_direction)
192-
193184
# # set extreme values of the axis helper
194185
# if nth_coord == 1:
195186
# axis.get_helper().set_extremes(*self._extremes[:2])
196187
# elif nth_coord == 0:
197188
# axis.get_helper().set_extremes(*self._extremes[2:])
198-
199189
# return axis
200190

201191
def _update_grid(self, x1, y1, x2, y2):
@@ -291,8 +281,6 @@ def adjust_axes_lim(self):
291281
self.set_ylim(bbox.ymin, bbox.ymax)
292282

293283

294-
floatingaxes_class_factory = cbook._make_class_factory(
295-
FloatingAxesBase, "Floating{}")
296-
FloatingAxes = floatingaxes_class_factory(
297-
host_axes_class_factory(axislines.Axes))
284+
floatingaxes_class_factory = cbook._make_class_factory(FloatingAxesBase, "Floating{}")
285+
FloatingAxes = floatingaxes_class_factory(host_axes_class_factory(axislines.Axes))
298286
FloatingSubplot = FloatingAxes

lib/mpl_toolkits/axisartist/grid_helper_curvelinear.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,8 @@ def update_grid_finder(self, aux_trans=None, **kwargs):
272272
self._old_limits = None # Force revalidation.
273273

274274
@_api.make_keyword_only("3.9", "nth_coord")
275-
def new_fixed_axis(self, loc,
276-
nth_coord=None,
277-
axis_direction=None,
278-
offset=None,
279-
axes=None):
275+
def new_fixed_axis(
276+
self, loc, nth_coord=None, axis_direction=None, offset=None, axes=None):
280277
if axes is None:
281278
axes = self.axes
282279
if axis_direction is None:
@@ -287,11 +284,7 @@ def new_fixed_axis(self, loc,
287284
# the floating_axig.GridHelperCurveLinear subclass?
288285
return axisline
289286

290-
def new_floating_axis(self, nth_coord,
291-
value,
292-
axes=None,
293-
axis_direction="bottom"
294-
):
287+
def new_floating_axis(self, nth_coord, value, axes=None, axis_direction="bottom"):
295288
if axes is None:
296289
axes = self.axes
297290
helper = FloatingAxisArtistHelper(
@@ -317,22 +310,15 @@ def get_gridlines(self, which="major", axis="both"):
317310
return grid_lines
318311

319312
def get_tick_iterator(self, nth_coord, axis_side, minor=False):
320-
321-
# axisnr = dict(left=0, bottom=1, right=2, top=3)[axis_side]
322313
angle_tangent = dict(left=90, right=90, bottom=0, top=0)[axis_side]
323-
# angle = [0, 90, 180, 270][axisnr]
324314
lon_or_lat = ["lon", "lat"][nth_coord]
325315
if not minor: # major ticks
326-
for (xy, a), l in zip(
316+
for (xy, angle_normal), l in zip(
327317
self._grid_info[lon_or_lat]["tick_locs"][axis_side],
328318
self._grid_info[lon_or_lat]["tick_labels"][axis_side]):
329-
angle_normal = a
330319
yield xy, angle_normal, angle_tangent, l
331320
else:
332-
for (xy, a), l in zip(
321+
for (xy, angle_normal), l in zip(
333322
self._grid_info[lon_or_lat]["tick_locs"][axis_side],
334323
self._grid_info[lon_or_lat]["tick_labels"][axis_side]):
335-
angle_normal = a
336324
yield xy, angle_normal, angle_tangent, ""
337-
# for xy, a, l in self._grid_info[lon_or_lat]["ticks"][axis_side]:
338-
# yield xy, a, ""

0 commit comments

Comments
 (0)