Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Don't fail on empty autoscale_None. #9195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,15 +961,17 @@ def autoscale(self, A):
"""
Set *vmin*, *vmax* to min, max of *A*.
"""
self.vmin = np.ma.min(A)
self.vmax = np.ma.max(A)
A = np.asanyarray(A)
self.vmin = A.min()
self.vmax = A.max()

def autoscale_None(self, A):
' autoscale only None-valued vmin or vmax'
if self.vmin is None and np.size(A) > 0:
self.vmin = np.ma.min(A)
if self.vmax is None and np.size(A) > 0:
self.vmax = np.ma.max(A)
"""autoscale only None-valued vmin or vmax."""
A = np.asanyarray(A)
if self.vmin is None and A.size:
self.vmin = A.min()
if self.vmax is None and A.size:
self.vmax = A.max()

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

def autoscale_None(self, A):
' autoscale only None-valued vmin or vmax'
"""autoscale only None-valued vmin or vmax."""
if self.vmin is not None and self.vmax is not None:
return
A = np.ma.masked_less_equal(A, 0, copy=False)
if self.vmin is None:
self.vmin = np.ma.min(A)
if self.vmax is None:
self.vmax = np.ma.max(A)
if self.vmin is None and A.size:
self.vmin = A.min()
if self.vmax is None and A.size:
self.vmax = A.max()


class SymLogNorm(Normalize):
Expand Down Expand Up @@ -1153,13 +1155,14 @@ def autoscale(self, A):
self._transform_vmin_vmax()

def autoscale_None(self, A):
""" autoscale only None-valued vmin or vmax """
"""autoscale only None-valued vmin or vmax."""
if self.vmin is not None and self.vmax is not None:
pass
if self.vmin is None:
self.vmin = np.ma.min(A)
if self.vmax is None:
self.vmax = np.ma.max(A)
A = np.asanyarray(A)
if self.vmin is None and A.size:
self.vmin = A.min()
if self.vmax is None and A.size:
self.vmax = A.max()
self._transform_vmin_vmax()


Expand Down Expand Up @@ -1223,20 +1226,19 @@ def autoscale(self, A):
self.vmin = 0
warnings.warn("Power-law scaling on negative values is "
"ill-defined, clamping to 0.")

self.vmax = np.ma.max(A)

def autoscale_None(self, A):
' autoscale only None-valued vmin or vmax'
if self.vmin is None and np.size(A) > 0:
self.vmin = np.ma.min(A)
"""autoscale only None-valued vmin or vmax."""
A = np.asanyarray(A)
if self.vmin is None and A.size:
self.vmin = A.min()
if self.vmin < 0:
self.vmin = 0
warnings.warn("Power-law scaling on negative values is "
"ill-defined, clamping to 0.")

if self.vmax is None and np.size(A) > 0:
self.vmax = np.ma.max(A)
if self.vmax is None and A.size:
self.vmax = A.max()


class BoundaryNorm(Normalize):
Expand Down
28 changes: 16 additions & 12 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@
unicode_literals)

import six

from copy import copy
import io
import os
import warnings

import numpy as np
from numpy import ma
from numpy.testing import assert_array_equal

from matplotlib.testing.decorators import image_comparison
from matplotlib import (
colors, image as mimage, mlab, patches, pyplot as plt,
rc_context, rcParams)
from matplotlib.image import (AxesImage, BboxImage, FigureImage,
NonUniformImage, PcolorImage)
from matplotlib.testing.decorators import image_comparison
from matplotlib.transforms import Bbox, Affine2D, TransformedBbox
from matplotlib import rcParams, rc_context
from matplotlib import patches
import matplotlib.pyplot as plt

from matplotlib import mlab
import pytest

from copy import copy
from numpy import ma
import matplotlib.image as mimage
import matplotlib.colors as colors


try:
from PIL import Image
Expand Down Expand Up @@ -789,9 +786,16 @@ def test_imshow_flatfield():
im.set_clim(.5, 1.5)


def test_empty_imshow():
@pytest.mark.parametrize(
"make_norm",
[colors.Normalize,
colors.LogNorm,
lambda: colors.SymLogNorm(1),
lambda: colors.PowerNorm(1)])
@pytest.mark.filterwarnings("ignore:Attempting to set identical left==right")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future reference, this was added in pytest 3.2 and our docs only say we require 3.0, so it probably shouldn't be relied upon.

def test_empty_imshow(make_norm):
fig, ax = plt.subplots()
im = ax.imshow([[]])
im = ax.imshow([[]], norm=make_norm())
im.set_extent([-5, 5, -5, 5])
fig.canvas.draw()

Expand Down