10000 Merge remote-tracking branch 'upstream/v1.3.x' · matplotlib/matplotlib@38fd462 · GitHub
[go: up one dir, main page]

Skip to content

Commit 38fd462

Browse files
committed
Merge remote-tracking branch 'upstream/v1.3.x'
Conflicts: lib/matplotlib/tests/test_colors.py
2 parents 263c1b9 + d1dd52a commit 38fd462

File tree

7 files changed

+116
-22
lines changed

7 files changed

+116
-22
lines changed

lib/matplotlib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,7 @@ def tk_window_focus():
13171317
'matplotlib.tests.test_dates',
13181318
'matplotlib.tests.test_delaunay',
13191319
'matplotlib.tests.test_figure',
1320+
'matplotlib.tests.test_gridspec',
13201321
'matplotlib.tests.test_image',
13211322
'matplotlib.tests.test_legend',
13221323
'matplotlib.tests.test_lines',

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,6 @@ def destroy(self, *args):
434434
self.canvas.destroy()
435435
if self.toolbar:
436436
self.toolbar.destroy()
437-
self.__dict__.clear() #Is this needed? Other backends don't have it.
438437

439438
if Gcf.get_num_fig_managers()==0 and \
440439
not matplotlib.is_interactive() and \

lib/matplotlib/backends/backend_pgf.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,9 @@ def make_pdf_to_png_converter():
174174
except:
175175
pass
176176
# check for ghostscript
177-
try:
178-
gs = "gs" if sys.platform != "win32" else "gswin32c"
179-
check_output([gs, "-v"], stderr=subprocess.STDOUT)
177+
gs, ver = mpl.checkdep_ghostscript()
178+
if gs:
180179
tools_available.append("gs")
181-
except:
182-
pass
183180

184181
# pick converter
185182
if "pdftocairo" in tools_available:

lib/matplotlib/colors.py

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,39 +1236,98 @@ def inverse(self, value):
12361236

12371237
def rgb_to_hsv(arr):
12381238
"""
1239-
convert rgb values in a numpy array to hsv values
1240-
input and output arrays should have shape (M,N,3)
1239+
convert float rgb values (in the range [0, 1]), in a numpy array to hsv
1240+
values.
1241+
1242+
Parameters
1243+
----------
1244+
arr : (..., 3) array-like
1245+
All values must be in the range [0, 1]
1246+
1247+
Returns
1248+
-------
1249+
hsv : (..., 3) ndarray
1250+
Colors converted to hsv values in range [0, 1]
12411251
"""
1242-
out = np.zeros(arr.shape, dtype=np.float)
1252+
# make sure it is an ndarray
1253+
arr = np.asarray(arr)
1254+
1255+
# check length of the last dimension, should be _some_ sort of rgb
1256+
if arr.shape[-1] != 3:
1257+
raise ValueError("Last dimension of input array must be 3; "
1258+
"shape {shp} was found.".format(shp=arr.shape))
1259+
1260+
in_ndim = arr.ndim
1261+
if arr.ndim == 1:
1262+
arr = np.array(arr, ndmin=2)
1263+
1264+
# make sure we don't have an int image
1265+
if arr.dtype.kind in ('iu'):
1266+
arr = arr.astype(np.float32)
1267+
1268+
out = np.zeros_like(arr)
12431269
arr_max = arr.max(-1)
12441270
ipos = arr_max > 0
12451271
delta = arr.ptp(-1)
12461272
s = np.zeros_like(delta)
12471273
s[ipos] = delta[ipos] / arr_max[ipos]
12481274
ipos = delta > 0
12491275
# red is max
1250-
idx = (arr[:, :, 0] == arr_max) & ipos
1276+
idx = (arr[..., 0] == arr_max) & ipos
12511277
out[idx, 0] = (arr[idx, 1] - arr[idx, 2]) / delta[idx]
12521278
# green is max
1253-
idx = (arr[:, :, 1] == arr_max) & ipos
1279+
idx = (arr[..., 1] == arr_max) & ipos
12541280
out[idx, 0] = 2. + (arr[idx, 2] - arr[idx, 0]) / delta[idx]
12551281
# blue is max
1256-
idx = (arr[:, :, 2] == arr_max) & ipos
1282+
idx = (arr[..., 2] == arr_max) & ipos
12571283
out[idx, 0] = 4. + (arr[idx, 0] - arr[idx, 1]) / delta[idx]
1258-
out[:, :, 0] = (out[:, :, 0] / 6.0) % 1.0
1259-
out[:, :, 1] = s
1260-
out[:, :, 2] = arr_max
1284+
1285+
out[..., 0] = (out[..., 0] / 6.0) % 1.0
1286+
out[..., 1] = s
1287+
out[..., 2] = arr_max
1288+
1289+
if in_ndim == 1:
1290+
out.shape = (3,)
1291+
12611292
return out
12621293

12631294

12641295
def hsv_to_rgb(hsv):
12651296
"""
12661297
convert hsv values in a numpy array to rgb values
1267-
both input and output arrays have shape (M,N,3)
1298+
all values assumed to be in range [0, 1]
1299+
1300+
Parameters
1301+
----------
1302+
hsv : (..., 3) array-like
1303+
All values assumed to be in range [0, 1]
1304+
1305+
Returns
1306+
-------
1307+
rgb : (..., 3) ndarray
1308+
Colors converted to RGB values in range [0, 1]
12681309
"""
1269-
h = hsv[:, :, 0]
1270-
s = hsv[:, :, 1]
1271-
v = hsv[:, :, 2]
1310+
# make sure it is an ndarray
1311+
hsv = np.asarray(hsv)
1312+
1313+
# check length of the last dimension, should be _some_ sort of rgb
1314+
if hsv.shape[-1] != 3:
1315+
raise ValueError("Last dimension of input array must be 3; "
1316+
"shape {shp} was found.".format(shp=hsv.shape))
1317+
1318+
# if we got pased a 1D array, try to treat as
1319+
# a single color and reshape as needed
1320+
in_ndim = hsv.ndim
1321+
if in_ndim == 1:
1322+
hsv = np.array(hsv, ndmin=2)
1323+
1324+
# make sure we don't have an int image
1325+
if hsv.dtype.kind in ('iu'):
1326+
hsv = hsv.astype(np.float32)
1327+
1328+
h = hsv[..., 0]
1329+
s = hsv[..., 1]
1330+
v = hsv[..., 2]
12721331

12731332
r = np.empty_like(h)
12741333
g = np.empty_like(h)
@@ -1316,9 +1375,13 @@ def hsv_to_rgb(hsv):
13161375
b[idx] = v[idx]
13171376

13181377
rgb = np.empty_like(hsv)
1319-
rgb[:, :, 0] = r
1320-
rgb[:, :, 1] = g
1321-
rgb[:, :, 2] = b
1378+
rgb[..., 0] = r
1379+
rgb[..., 1] = g
1380+
rgb[..., 2] = b
1381+
1382+
if in_ndim == 1:
1383+
rgb.shape = (3, )
1384+
13221385
return rgb
13231386

13241387

lib/matplotlib/gridspec.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,19 @@ def get_topmost_subplotspec(self):
461461
return gridspec.get_topmost_subplotspec()
462462
else:
463463
return self
464+
465+
def __eq__(self, other):
466+
# check to make sure other has the attributes
467+
# we need to do the comparison
468+
if not (hasattr(other, '_gridspec') and
469+
hasattr(other, 'num1') and
470+
hasattr(other, 'num2')):
471+
return False
472+
return all((self._gridspec == other._gridspec,
473+
self.num1 == other.num1,
474+
self.num2 == other.num2))
475+
476+
def __hash__(self):
477+
return (hash(self._gridspec) ^
478+
hash(self.num1) ^
479+
hash(self.num2))

lib/matplotlib/tests/test_colors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ def test_cmap_and_norm_from_levels_and_colors2():
184184
assert_raises(ValueError, mcolors.from_levels_and_colors, levels, colors)
185185

186186

187+
def test_rgb_hsv_round_trip():
188+
for a_shape in [(500, 500, 3), (500, 3), (1, 3), (3,)]:
189+
np.random.seed(0)
190+
tt = np.random.random(a_shape)
191+
assert_array_almost_equal(tt,
192+
mcolors.hsv_to_rgb(mcolors.rgb_to_hsv(tt)))
193+
assert_array_almost_equal(tt,
194+
mcolors.rgb_to_hsv(mcolors.hsv_to_rgb(tt)))
195+
196+
187197
@cleanup
188198
def test_autoscale_masked():
189199
# Test for #2336. Previously fully masked data would trigger a ValueError.

lib/matplotlib/tests/test_gridspec.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import matplotlib.gridspec as gridspec
2+
from nose.tools import assert_equal
3+
4+
5+
def test_equal():
6+
gs = gridspec.GridSpec(2, 1)
7+
assert_equal(gs[0, 0], gs[0, 0])
8+
assert_equal(gs[:, 0], gs[:, 0])

0 commit comments

Comments
 (0)
0