Skip to content

Commit f945eca

Browse files
authored
Merge pull request matplotlib#9195 from anntzer/empty-lognorm
Don't fail on empty autoscale_None.
2 parents 61714e7 + ae57ee6 commit f945eca

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

lib/matplotlib/colors.py

+26-24
Original file line numberDiff line numberDiff line change
@@ -961,15 +961,17 @@ def autoscale(self, A):
961961
"""
962962
Set *vmin*, *vmax* to min, max of *A*.
963963
"""
964-
self.vmin = np.ma.min(A)
965-
self.vmax = np.ma.max(A)
964+
A = np.asanyarray(A)
965+
self.vmin = A.min()
966+
self.vmax = A.max()
966967

967968
def autoscale_None(self, A):
968-
' autoscale only None-valued vmin or vmax'
969-
if self.vmin is None and np.size(A) > 0:
970-
self.vmin = np.ma.min(A)
971-
if self.vmax is None and np.size(A) > 0:
972-
self.vmax = np.ma.max(A)
969+
"""autoscale only None-valued vmin or vmax."""
970+
A = np.asanyarray(A)
971+
if self.vmin is None and A.size:
972+
self.vmin = A.min()
973+
if self.vmax is None and A.size:
974+
self.vmax = A.max()
973975

974976
def scaled(self):
975977
'return true if vmin and vmax set'
@@ -1037,14 +1039,14 @@ def autoscale(self, A):
10371039
self.vmax = np.ma.max(A)
10381040

10391041
def autoscale_None(self, A):
1040-
' autoscale only None-valued vmin or vmax'
1042+
"""autoscale only None-valued vmin or vmax."""
10411043
if self.vmin is not None and self.vmax is not None:
10421044
return
10431045
A = np.ma.masked_less_equal(A, 0, copy=False)
1044-
if self.vmin is None:
1045-
self.vmin = np.ma.min(A)
1046-
if self.vmax is None:
1047-
self.vmax = np.ma.max(A)
1046+
if self.vmin is None and A.size:
1047+
self.vmin = A.min()
1048+
if self.vmax is None and A.size:
1049+
self.vmax = A.max()
10481050

10491051

10501052
class SymLogNorm(Normalize):
@@ -1153,13 +1155,14 @@ def autoscale(self, A):
11531155
self._transform_vmin_vmax()
11541156

11551157
def autoscale_None(self, A):
1156-
""" autoscale only None-valued vmin or vmax """
1158+
"""autoscale only None-valued vmin or vmax."""
11571159
if self.vmin is not None and self.vmax is not None:
11581160
pass
1159-
if self.vmin is None:
1160-
self.vmin = np.ma.min(A)
1161-
if self.vmax is None:
1162-
self.vmax = np.ma.max(A)
1161+
A = np.asanyarray(A)
1162+
if self.vmin is None and A.size:
1163+
self.vmin = A.min()
1164+
if self.vmax is None and A.size:
1165+
self.vmax = A.max()
11631166
self._transform_vmin_vmax()
11641167

11651168

@@ -1223,20 +1226,19 @@ def autoscale(self, A):
12231226
self.vmin = 0
12241227
warnings.warn("Power-law scaling on negative values is "
12251228
"ill-defined, clamping to 0.")
1226-
12271229
self.vmax = np.ma.max(A)
12281230

12291231
def autoscale_None(self, A):
1230-
' autoscale only None-valued vmin or vmax'
1231-
if self.vmin is None and np.size(A) > 0:
1232-
self.vmin = np.ma.min(A)
1232+
"""autoscale only None-valued vmin or vmax."""
1233+
A = np.asanyarray(A)
1234+
if self.vmin is None and A.size:
1235+
self.vmin = A.min()
12331236
if self.vmin < 0:
12341237
self.vmin = 0
12351238
warnings.warn("Power-law scaling on negative values is "
12361239
"ill-defined, clamping to 0.")
1237-
1238-
if self.vmax is None and np.size(A) > 0:
1239-
self.vmax = np.ma.max(A)
1240+
if self.vmax is None and A.size:
1241+
self.vmax = A.max()
12401242

12411243

12421244
class BoundaryNorm(Normalize):

lib/matplotlib/tests/test_image.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,26 @@
22
unicode_literals)
33

44
import six
5+
6+
from copy import copy
57
import io
68
import os
79
import warnings
810

911
import numpy as np
12+
from numpy import ma
1013
from numpy.testing import assert_array_equal
1114

12-
from matplotlib.testing.decorators import image_comparison
15+
from matplotlib import (
16+
colors, image as mimage, mlab, patches, pyplot as plt,
17+
rc_context, rcParams)
1318
from matplotlib.image import (AxesImage, BboxImage, FigureImage,
1419
NonUniformImage, PcolorImage)
20+
from matplotlib.testing.decorators import image_comparison
1521
from matplotlib.transforms import Bbox, Affine2D, TransformedBbox
16-
from matplotlib import rcParams, rc_context
17-
from matplotlib import patches
18-
import matplotlib.pyplot as plt
1922

20-
from matplotlib import mlab
2123
import pytest
2224

23-
from copy import copy
24-
from numpy import ma
25-
import matplotlib.image as mimage
26-
import matplotlib.colors as colors
27-
2825

2926
try:
3027
from PIL import Image
@@ -789,9 +786,16 @@ def test_imshow_flatfield():
789786
im.set_clim(.5, 1.5)
790787

791788

792-
def test_empty_imshow():
789+
@pytest.mark.parametrize(
790+
"make_norm",
791+
[colors.Normalize,
792+
colors.LogNorm,
793+
lambda: colors.SymLogNorm(1),
794+
lambda: colors.PowerNorm(1)])
795+
@pytest.mark.filterwarnings("ignore:Attempting to set identical left==right")
796+
def test_empty_imshow(make_norm):
793797
fig, ax = plt.subplots()
794-
im = ax.imshow([[]])
798+
im = ax.imshow([[]], norm=make_norm())
795799
im.set_extent([-5, 5, -5, 5])
796800
fig.canvas.draw()
797801

0 commit comments

Comments
 (0)