8000 Cleanup shape manipulations. · matplotlib/matplotlib@ae2432c · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit ae2432c

Browse files
committed
Cleanup shape manipulations.
- In boxplot() examples, we can just pass 1d arrays, no need to reshape them to columns. - In image.py, prefer squeeze() to assignment to shape as the latter modifies the input in place.
1 parent 439a122 commit ae2432c

File tree

4 files changed

+16
-26
lines changed

4 files changed

+16
-26
lines changed

examples/pyplots/boxplot_demo_pyplot.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,14 @@
6565
flier_high = np.random.rand(10) * 100 + 100
6666
flier_low = np.random.rand(10) * -100
6767
d2 = np.concatenate((spread, center, flier_high, flier_low))
68-
data.shape = (-1, 1)
69-
d2.shape = (-1, 1)
7068

7169
###############################################################################
7270
# Making a 2-D array only works if all the columns are the
7371
# same length. If they are not, then use a list instead.
7472
# This is actually more efficient because boxplot converts
7573
# a 2-D array into a list of vectors internally anyway.
7674

77-
data = [data, d2, d2[::2, 0]]
75+
data = [data, d2, d2[::2]]
7876
fig7, ax7 = plt.subplots()
7977
ax7.set_title('Multiple Samples with Different sizes')
8078
ax7.boxplot(data)

examples/statistics/boxplot_demo.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,11 @@
6060
flier_high = np.random.rand(10) * 100 + 100
6161
flier_low = np.random.rand(10) * -100
6262
d2 = np.concatenate((spread, center, flier_high, flier_low))
63-
data.shape = (-1, 1)
64-
d2.shape = (-1, 1)
6563
# Making a 2-D array only works if all the columns are the
6664
# same length. If they are not, then use a list instead.
6765
# This is actually more efficient because boxplot converts
6866
# a 2-D array into a list of vectors internally anyway.
69-
data = [data, d2, d2[::2, 0]]
67+
data = [data, d2, d2[::2]]
7068

7169
# Multiple box plots on one Axes
7270
fig, ax = plt.subplots()

lib/matplotlib/image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ def set_data(self, x, y, A):
10691069
raise TypeError("3D arrays must have three (RGB) "
10701070
"or four (RGBA) color components")
10711071
if A.ndim == 3 and A.shape[2] == 1:
1072-
A.shape = A.shape[0:2]
1072+
A = A.squeeze(axis=-1)
10731073
self._A = A
10741074
self._Ax = x
10751075
self._Ay = y
@@ -1230,7 +1230,7 @@ def set_data(self, x, y, A):
12301230
if A.ndim not in [2, 3]:
12311231
raise ValueError("A must be 2D or 3D")
12321232
if A.ndim == 3 and A.shape[2] == 1:
1233-
A.shape = A.shape[:2]
1233+
A = A.squeeze(axis=-1)
12341234
self._is_grayscale = False
12351235
if A.ndim == 3:
12361236
if A.shape[2] in [3, 4]:

lib/matplotlib/tests/test_image.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -657,17 +657,6 @@ def test_jpeg_alpha():
657657
assert corner_pixel == (254, 0, 0)
658658

659659

660-
def test_nonuniformimage_setdata():
661-
ax = plt.gca()
662-
im = NonUniformImage(ax)
663-
x = np.arange(3, dtype=float)
664-
y = np.arange(4, dtype=float)
665-
z = np.arange(12, dtype=float).reshape((4, 3))
666-
im.set_data(x, y, z)
667-
x[0] = y[0] = z[0, 0] = 9.9
668-
assert im._A[0, 0] == im._Ax[0] == im._Ay[0] == 0, 'value changed'
669-
670-
671660
def test_axesimage_setdata():
672661
ax = plt.gca()
673662
im = AxesImage(ax)
@@ -686,15 +675,20 @@ def test_figureimage_setdata():
686675
assert im._A[0, 0] == 0, 'value changed'
687676

688677

689-
def test_pcolorimage_setdata():
678+
@pytest.mark.parametrize(
679+
"image_cls,x,y,a", [
680+
(NonUniformImage,
681+
np.arange(3.), np.arange(4.), np.arange(12.).reshape((4, 3))),
682+
(PcolorImage,
683+
np.arange(3.), np.arange(4.), np.arange(6.).reshape((3, 2))),
684+
])
685+
def test_setdata_xya(image_cls, x, y, a):
690686
ax = plt.gca()
691-
im = PcolorImage(ax)
692-
x = np.arange(3, dtype=float)
693-
y = np.arange(4, dtype=float)
694-
z = np.arange(6, dtype=float).reshape((3, 2))
695-
im.set_data(x, y, z)
696-
x[0] = y[0] = z[0, 0] = 9.9
687+
im = image_cls(ax)
688+
im.set_data(x, y, a)
689+
x[0] = y[0] = a[0, 0] = 9.9
697690
assert im._A[0, 0] == im._Ax[0] == im._Ay[0] == 0, 'value changed'
691+
im.set_data(x, y, a.reshape((*a.shape, -1))) # Just a smoketest.
698692

699693

700694
def test_minimized_rasterized():

0 commit comments

Comments
 (0)
0