8000 Replace 1-tuples by scalars where possible. · matplotlib/matplotlib@2527617 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2527617

Browse files
committed
Replace 1-tuples by scalars where possible.
`(x,)` is a bit unsightly when `x` would work as well...
1 parent 57e1eac commit 2527617

File tree

14 files changed

+46
-58
lines changed

14 files changed

+46
-58
lines changed

examples/images_contours_and_fields/contour_label_demo.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@
3636

3737
class nf(float):
3838
def __repr__(self):
39-
str = '%.1f' % (self.__float__(),)
40-
if str[-1] == '0':
41-
return '%.0f' % self.__float__()
42-
else:
43-
return '%.1f' % self.__float__()
39+
s = f'{self:.1f}'
40+
return f'{self:.0f}' if s[-1] == '0' else s
4441

4542

4643
# Basic contour plot

examples/misc/transoffset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
x=0.05, y=0.10, units='inches')
3939

4040
for x, y in zip(xs, ys):
41-
plt.plot((x,), (y,), 'ro')
41+
plt.plot(x, y, 'ro')
4242
plt.text(x, y, '%d, %d' % (int(x), int(y)), transform=trans_offset)
4343

4444

@@ -49,7 +49,7 @@
4949
y=6, units='dots')
5050

5151
for x, y in zip(xs, ys):
52-
plt.polar((x,), (y,), 'ro')
52+
plt.polar(x, y, 'ro')
5353
plt.text(x, y, '%d, %d' % (int(x), int(y)),
5454
transform=trans_offset,
5555
horizontalalignment='center',

examples/mplot3d/lorenz_attractor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def lorenz(x, y, z, s=10, r=28, b=2.667):
3838
num_steps = 10000
3939

4040
# Need one more for the initial values
41-
xs = np.empty((num_steps + 1,))
42-
ys = np.empty((num_steps + 1,))
43-
zs = np.empty((num_steps + 1,))
41+
xs = np.empty(num_steps + 1)
42+
ys = np.empty(num_steps + 1)
43+
zs = np.empty(num_steps + 1)
4444

4545
# Set initial values
4646
xs[0], ys[0], zs[0] = (0., 1., 1.05)

lib/matplotlib/colorbar.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ def _process_values(self, b=None):
790790
if self.values is not None:
791791
self._values = np.array(self.values)
792792
if self.boundaries is None:
793-
b = np.zeros(len(self.values) + 1, 'd')
793+
b = np.zeros(len(self.values) + 1)
794794
b[1:-1] = 0.5 * (self._values[:-1] - self._values[1:])
795795
b[0] = 2.0 * b[1] - b[2]
796796
b[-1] = 2.0 * b[-2] - b[-3]
@@ -802,7 +802,7 @@ def _process_values(self, b=None):
802802
# make reasonable ones based on cmap and norm.
803803
if isinstance(self.norm, colors.NoNorm):
804804
b = self._uniform_y(self.cmap.N + 1) * self.cmap.N - 0.5
805-
v = np.zeros((len(b) - 1,), dtype=np.int16)
805+
v = np.zeros(len(b) - 1, dtype=np.int16)
806806
v[self._inside] = np.arange(self.cmap.N, dtype=np.int16)
807807
if self._extend_lower():
808808
v[0] = -1
@@ -818,7 +818,7 @@ def _process_values(self, b=None):
818818
if self._extend_upper():
819819
b = b + [b[-1] + 1]
820820
b = np.array(b)
821-
v = np.zeros((len(b) - 1,), dtype=float)
821+
v = np.zeros(len(b) - 1)
822822
bi = self.norm.boundaries
823823
v[self._inside] = 0.5 * (bi[:-1] + bi[1:])
824824
if self._extend_lower():

lib/matplotlib/colors.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,15 @@ def makeMappingArray(N, data, gamma=1.0):
412412
raise ValueError("data mapping points must have x in increasing order")
413413
# begin generation of lookup table
414414
x = x * (N - 1)
415-
lut = np.zeros((N,), float)
416415
xind = (N - 1) * np.linspace(0, 1, N) ** gamma
417416
ind = np.searchsorted(x, xind)[1:-1]
418417

419418
distance = (xind[1:-1] - x[ind - 1]) / (x[ind] - x[ind - 1])
420-
lut[1:-1] = distance * (y0[ind] - y1[ind - 1]) + y1[ind - 1]
421-
lut[0] = y1[0]
422-
lut[-1] = y0[-1]
419+
lut = np.concatenate([
420+
[y1[0]],
421+
distance * (y0[ind] - y1[ind - 1]) + y1[ind - 1],
422+
[y0[-1]],
423+
])
423424
# ensure that the lut is confined to values between 0 and 1 by clipping it
424425
return np.clip(lut, 0.0, 1.0)
425426

@@ -543,8 +544,7 @@ def __call__(self, X, alpha=None, bytes=False):
543544
# If the bad value is set to have a color, then we
6377
544545
# override its alpha just as for any other value.
545546

546-
rgba = np.empty(shape=xa.shape + (4,), dtype=lut.dtype)
547-
lut.take(xa, axis=0, mode='clip', out=rgba)
547+
rgba = lut.take(xa, axis=0, mode='clip')
548548
if vtype == 'scalar':
549549
rgba = tuple(rgba[0, :])
550550
return rgba

lib/matplotlib/hatch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def get_path(hatchpattern, density=6):
201201
return Path(np.empty((0, 2)))
202202

203203
vertices = np.empty((num_vertices, 2))
204-
codes = np.empty((num_vertices,), np.uint8)
204+
codes = np.empty(num_vertices, Path.code_type)
205205

206206
cursor = 0
207207
for pattern in patterns:

lib/matplotlib/mlab.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,12 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
516516
# zero pad x and y up to NFFT if they are shorter than NFFT
517517
if len(x) < NFFT:
518518
n = len(x)
519-
x = np.resize(x, (NFFT,))
519+
x = np.resize(x, NFFT)
520520
x[n:] = 0
521521

522522
if not same_data and len(y) < NFFT:
523523
n = len(y)
524-
y = np.resize(y, (NFFT,))
524+
y = np.resize(y, NFFT)
525525
y[n:] = 0
526526

527527
if pad_to is None:
@@ -1598,7 +1598,7 @@ def evaluate(self, points):
15981598
raise ValueError("points have dimension {}, dataset has dimension "
15991599
"{}".format(dim, self.dim))
16001600

1601-
result = np.zeros((num_m,), dtype=float)
1601+
result = np.zeros(num_m)
16021602

16031603
if num_m >= self.num_dp:
16041604
# there are more points than data, so loop over data

lib/matplotlib/projections/polar.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,12 +1123,10 @@ def set_theta_direction(self, direction):
11231123
Theta increases in the counterclockwise direction
11241124
"""
11251125
mtx = self._direction.get_matrix()
1126-
if direction in ('clockwise',):
1126+
if direction in ('clockwise', -1):
11271127
mtx[0, 0] = -1
1128-
elif direction in ('counterclockwise', 'anticlockwise'):
1128+
elif direction in ('counterclockwise', 'anticlockwise', 1):
11291129
mtx[0, 0] = 1
1130-
elif direction in (1, -1):
1131-
mtx[0, 0] = direction
11321130
else:
11331131
raise ValueError(
11341132
"direction must be 1, -1, clockwise or counterclockwise")

lib/matplotlib/quiver.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,8 @@ def _init(self):
306306
# Hack: save and restore the Umask
307307
_mask = self.Q.Umask
308308
self.Q.Umask = ma.nomask
309-
self.verts = self.Q._make_verts(np.array([self.U]),
310-
np.zeros((1,)),
311-
self.angle)
309+
self.verts = self.Q._make_verts(
310+
np.array([self.U]), np.zeros(1), self.angle)
312311
self.Q.Umask = _mask
313312
self.Q.pivot = _pivot
314313
kw = self.Q.polykw

lib/matplotlib/tests/test_axes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -630,13 +630,13 @@ def test_const_xy():
630630
fig = plt.figure()
631631

632632
plt.subplot(311)
633-
plt.plot(np.arange(10), np.ones((10,)))
633+
plt.plot(np.arange(10), np.ones(10))
634634

635635
plt.subplot(312)
636-
plt.plot(np.ones((10,)), np.arange(10))
636+
plt.plot(np.ones(10), np.arange(10))
637637

638638
plt.subplot(313)
639-
plt.plot(np.ones((10,)), np.ones((10,)), 'o')
639+
plt.plot(np.ones(10), np.ones(10), 'o')
640640

641641

642642
@image_comparison(baseline_images=['polar_wrap_180', 'polar_wrap_360'],
@@ -5198,7 +5198,7 @@ def test_violin_point_mass():
51985198

51995199

52005200
def generate_errorbar_inputs():
5201-
base_xy = cycler('x', [np.arange(5)]) + cycler('y', [np.ones((5, ))])
5201+
base_xy = cycler('x', [np.arange(5)]) + cycler('y', [np.ones(5)])
52025202
err_cycler = cycler('err', [1,
52035203
[1, 1, 1, 1, 1],
52045204
[[1, 1, 1, 1, 1],

lib/matplotlib/tests/test_mlab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def check_window_apply_repeat(self, x, window, NFFT, noverlap):
210210
if np.iterable(window):
211211
windowVals = window
212212
else:
213-
windowVals = window(np.ones((NFFT,), x.dtype))
213+
windowVals = window(np.ones(NFFT, x.dtype))
214214

215215
# do the ffts of the slices
216216
for i in range(n):

lib/matplotlib/tests/test_simplification.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_diamond():
4949

5050
def test_noise():
5151
np.random.seed(0)
52-
x = np.random.uniform(size=(50000,)) * 50
52+
x = np.random.uniform(size=50000) * 50
5353

5454
fig, ax = plt.subplots()
5555
p1 = ax.plot(x, solid_joinstyle='round', linewidth=2.0)
@@ -190,7 +190,7 @@ def test_angled_antiparallel(angle, offset):
190190
def test_sine_plus_noise():
191191
np.random.seed(0)
192192
x = (np.sin(np.linspace(0, np.pi * 2.0, 50000)) +
193-
np.random.uniform(size=(50000,)) * 0.01)
193+
np.random.uniform(size=50000) * 0.01)
194194

195195
fig, ax = plt.subplots()
196196
p1 = ax.plot(x, solid_joinstyle='round', linewidth=2.0)

lib/mpl_toolkits/axes_grid1/colorbar.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,8 @@ def _process_values(self, b=None):
596596
if b is not None:
597597
self._boundaries = np.asarray(b, dtype=float)
598598
if self.values is None:
599-
self._values = 0.5*(self._boundaries[:-1]
600-
+ self._boundaries[1:])
599+
self._values = (self._boundaries[:-1]
600+
+ self._boundaries[1:]) / 2
601601
if isinstance(self.norm, colors.NoNorm):
602602
self._values = (self._values + 0.00001).astype(np.int16)
603603
return
@@ -606,7 +606,7 @@ def _process_values(self, b=None):
606606
if self.values is not None:
607607
self._values = np.array(self.values)
608608
if self.boundaries is None:
609-
b = np.zeros(len(self.values)+1, 'd')
609+
b = np.zeros(len(self.values) + 1)
610610
b[1:-1] = 0.5*(self._values[:-1] - self._values[1:])
611611
b[0] = 2.0*b[1] - b[2]
612612
b[-1] = 2.0*b[-2] - b[-3]
@@ -617,22 +617,16 @@ def _process_values(self, b=None):
617617
# Neither boundaries nor values are specified;
618618
# make reasonable ones based on cmap and norm.
619619
if isinstance(self.norm, colors.NoNorm):
620-
b = self._uniform_y(self.cmap.N+1) * self.cmap.N - 0.5
621-
v = np.zeros((len(b)-1,), dtype=np.int16)
622-
v = np.arange(self.cmap.N, dtype=np.int16)
623-
self._boundaries = b
624-
self._values = v
620+
self._boundaries = (
621+
self._uniform_y(self.cmap.N + 1) * self.cmap.N - 0.5)
622+
self._values = np.arange(self.cmap.N, dtype=np.int16)
625623
return
626624
elif isinstance(self.norm, colors.BoundaryNorm):
627-
b = np.array(self.norm.boundaries)
628-
v = np.zeros((len(b)-1,), dtype=float)
629-
bi = self.norm.boundaries
630-
v = 0.5*(bi[:-1] + bi[1:])
631-
self._boundaries = b
632-
self._values = v
625+
self._boundaries = np.array(self.norm.boundaries)
626+
self._values = (self._boundaries[:-1] + self._boundaries[1:]) / 2
633627
return
634628
else:
635-
b = self._uniform_y(self.cmap.N+1)
629+
b = self._uniform_y(self.cmap.N + 1)
636630

637631
self._process_values(b)
638632

tools/memleak.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ def run_memleak_test(bench, iterations, report):
1818
starti = min(50, iterations // 2)
1919
endi = iterations
< 57AE /code>
2020

21-
malloc_arr = np.empty((endi,), dtype=np.int64)
22-
rss_arr = np.empty((endi,), dtype=np.int64)
23-
rss_peaks = np.empty((endi,), dtype=np.int64)
24-
nobjs_arr = np.empty((endi,), dtype=np.int64)
25-
garbage_arr = np.empty((endi,), dtype=np.int64)
26-
open_files_arr = np.empty((endi,), dtype=np.int64)
21+
malloc_arr = np.empty(endi, dtype=np.int64)
22+
rss_arr = np.empty(endi, dtype=np.int64)
23+
rss_peaks = np.empty(endi, dtype=np.int64)
24+
nobjs_arr = np.empty(endi, dtype=np.int64)
25+
garbage_arr = np.empty(endi, dtype=np.int64)
26+
open_files_arr = np.empty(endi, dtype=np.int64)
2727
rss_peak = 0
2828

2929
p = psutil.Process()

0 commit comments

Comments
 (0)
0