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

Skip to content

Commit 0178730

Browse files
committed
Bivariate works with imshow, pcolor, pcolormesh, pcolorfast
1 parent 7217536 commit 0178730

File tree

2 files changed

+115
-12
lines changed

2 files changed

+115
-12
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5141,12 +5141,30 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
51415141
if not self._hold:
51425142
self.cla()
51435143

5144-
if norm is not None and not isinstance(norm, mcolors.Normalize):
5145-
msg = "'norm' must be an instance of 'mcolors.Normalize'"
5144+
isNorm = isinstance(norm, (mcolors.Normalize, mcolors.BivariateNorm))
5145+
if norm is not None and not isNorm:
5146+
msg = "'norm' must be an instance of 'mcolors.Normalize' " \
5147+
"or 'mcolors.BivariateNorm'"
51465148
raise ValueError(msg)
5149+
51475150
if aspect is None:
51485151
aspect = rcParams['image.aspect']
51495152
self.set_aspect(aspect)
5153+
5154+
temp = np.asarray(X)
5155+
if (temp.ndim == 3 and isinstance(norm, mcolors.BivariateNorm) or
5156+
isinstance(cmap, mcolors.BivariateColormap)):
5157+
if cmap is None:
5158+
cmap = mcolors.BivariateColormap()
5159+
if norm is None:
5160+
norm = mcolors.BivariateNorm()
5161+
temp = norm(temp)
5162+
temp[0] = temp[0] * (cmap.N-1)
5163+
temp[1] = temp[1] * (cmap.N-1)
5164+
temp = temp.astype(int)
5165+
X = temp[0] + cmap.N * temp[1]
5166+
norm = mcolors.NoNorm()
5167+
51505168
im = mimage.AxesImage(self, cmap, norm, interpolation, origin, extent,
51515169
filternorm=filternorm, filterrad=filterrad,
51525170
resample=resample, **kwargs)
@@ -5173,7 +5191,6 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
51735191

51745192
@staticmethod
51755193
def _pcolorargs(funcname, *args, **kw):
5176-
# This takes one kwarg, allmatch.
51775194
# If allmatch is True, then the incoming X, Y, C must
51785195
# have matching dimensions, taking into account that
51795196
# X and Y can be 1-D rather than 2-D. This perfect
@@ -5186,9 +5203,25 @@ def _pcolorargs(funcname, *args, **kw):
51865203
# is False.
51875204

51885205
allmatch = kw.pop("allmatch", False)
5206+
norm = kw.pop("norm", None)
5207+
cmap = kw.pop("cmap", None)
51895208

51905209
if len(args) == 1:
51915210
C = np.asanyarray(args[0])
5211+
5212+
if (C.ndim == 3 and isinstance(norm, mcolors.BivariateNorm) or
5213+
isinstance(cmap, mcolors.BivariateColormap)):
5214+
if cmap is None:
5215+
cmap = mcolors.BivariateColormap()
5216+
if norm is None:
5217+
norm = mcolors.BivariateNorm()
5218+
C = norm(C)
5219+
C[0] = C[0] * (cmap.N-1)
5220+
C[1] = C[1] * (cmap.N-1)
5221+
C = C.astype(int)
5222+
C = C[0] + cmap.N * C[1]
5223+
C = np.array(C)
5224+
51925225
numRows, numCols = C.shape
51935226
if allmatch:
51945227
X, Y = np.meshgrid(np.arange(numCols), np.arange(numRows))
@@ -5200,6 +5233,18 @@ def _pcolorargs(funcname, *args, **kw):
52005233

52015234
if len(args) == 3:
52025235
X, Y, C = [np.asanyarray(a) for a in args]
5236+
if (C.ndim == 3 and isinstance(norm, mcolors.BivariateNorm) or
5237+
isinstance(cmap, mcolors.BivariateColormap)):
5238+
if cmap is None:
5239+
cmap = mcolors.BivariateColormap()
5240+
if norm is None:
5241+
norm = mcolors.BivariateNorm()
5242+
C = norm(C)
5243+
C[0] = C[0] * (cmap.N-1)
5244+
C[1] = C[1] * (cmap.N-1)
5245+
C = C.astype(int)
5246+
C = C[0] + cmap.N * C[1]
5247+
C = np.array(C)
52035248
numRows, numCols = C.shape
52045249
else:
52055250
raise TypeError(
@@ -5382,9 +5427,14 @@ def pcolor(self, *args, **kwargs):
53825427
vmin = kwargs.pop('vmin', None)
53835428
vmax = kwargs.pop('vmax', None)
53845429

5385-
X, Y, C = self._pcolorargs('pcolor', *args, allmatch=False)
5430+
kw = {'norm': norm, 'cmap': cmap, 'allmatch': False}
5431+
X, Y, C = self._pcolorargs('pcolor', *args, **kw)
53865432
Ny, Nx = X.shape
53875433

5434+
if (isinstance(norm, mcolors.BivariateNorm) or
5435+
isinstance(cmap, mcolors.BivariateColormap)):
5436+
norm = mcolors.NoNorm()
5437+
53885438
# unit conversion allows e.g. datetime objects as axis values
53895439
self._process_unit_info(xdata=X, ydata=Y, kwargs=kwargs)
53905440
X = self.convert_xunits(X)
@@ -5450,9 +5500,13 @@ def pcolor(self, *args, **kwargs):
54505500

54515501
collection.set_alpha(alpha)
54525502
collection.set_array(C)
5453-
if norm is not None and not isinstance(norm, mcolors.Normalize):
5454-
msg = "'norm' must be an instance of 'mcolors.Normalize'"
5503+
5504+
isNorm = isinstance(norm, (mcolors.Normalize, mcolors.BivariateNorm))
5505+
if norm is not None and not isNorm:
5506+
msg = "'norm' must be an instance of 'mcolors.Normalize' " \
5507+
"or 'mcolors.BivariateNorm'"
54555508
raise ValueError(msg)
5509+
54565510
collection.set_cmap(cmap)
54575511
collection.set_norm(norm)
54585512
collection.set_clim(vmin, vmax)
@@ -5582,9 +5636,14 @@ def pcolormesh(self, *args, **kwargs):
55825636

55835637
allmatch = (shading == 'gouraud')
55845638

5585-
X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
5639+
kw = {'norm': norm, 'cmap': cmap, 'allmatch': allmatch}
5640+
X, Y, C = self._pcolorargs('pcolormesh', *args, **kw)
55865641
Ny, Nx = X.shape
55875642

5643+
if (isinstance(norm, mcolors.BivariateNorm) or
5644+
isinstance(cmap, mcolors.BivariateColormap)):
5645+
norm = mcolors.NoNorm()
5646+
55885647
# unit conversion allows e.g. datetime objects as axis values
55895648
self._process_unit_info(xdata=X, ydata=Y, kwargs=kwargs)
55905649
X = self.convert_xunits(X)
@@ -5723,11 +5782,28 @@ def pcolorfast(self, *args, **kwargs):
57235782
cmap = kwargs.pop('cmap', None)
57245783
vmin = kwargs.pop('vmin', None)
57255784
vmax = kwargs.pop('vmax', None)
5726-
if norm is not None and not isinstance(norm, mcolors.Normalize):
5727-
msg = "'norm' must be an instance of 'mcolors.Normalize'"
5785+
isNorm = isinstance(norm, (mcolors.Normalize, mcolors.BivariateNorm))
5786+
if norm is not None and not isNorm:
5787+
msg = "'norm' must be an instance of 'mcolors.Normalize' " \
5788+
"or 'mcolors.BivariateNorm'"
57285789
raise ValueError(msg)
57295790

5730-
C = args[-1]
5791+
C = np.asarray(args[-1])
5792+
5793+
if (C.ndim == 3 and isinstance(norm, mcolors.BivariateNorm) or
5794+
isinstance(cmap, mcolors.BivariateColormap)):
5795+
if cmap is None:
5796+
cmap = mcolors.BivariateColormap()
5797+
if norm is None:
5798+
norm = mcolors.BivariateNorm()
5799+
C = norm(C)
5800+
C[0] = C[0] * (cmap.N-1)
5801+
C[1] = C[1] * (cmap.N-1)
5802+
C = C.astype(int)
5803+
C = C[0] + cmap.N * C[1]
5804+
C = np.array(C)
5805+
norm = mcolors.NoNorm()
5806+
57315807
nr, nc = C.shape
57325808
if len(args) == 1:
57335809
style = "image"

lib/matplotlib/colors.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,32 @@ def reversed(self, name=None):
858858
return ListedColormap(colors_r, name=name, N=self.N)
859859

860860

861+
class BivariateColormap(Colormap):
862+
def __init__(self, name, N=256):
863+
Colormap.__init__(self, name, N)
864+
865+
def _init(self):
866+
red = np.linspace(0, 1, self.N)
867+
green = np.linspace(0, 1, self.N)
868+
red_mesh, green_mesh = np.meshgrid(red, green)
869+
blue_mesh = np.zeros_like(red_mesh)
870+
alpha_mesh = np.ones_like(red_mesh)
871+
bivariate_cmap = np.dstack((red_mesh, green_mesh, blue_mesh, alpha_mesh))
872+
self._lut = np.vstack(bivariate_cmap)
873+
self._isinit = True
874+
self.N = self.N * self.N
875+
self._set_extremes()
876+
877+
def _resample(self, lutsize):
878+
"""
879+
Return a new color map with *lutsize x lutsize* entries.
880+
"""
881+
return BivariateColormap(self.name, lutsize)
882+
883+
def reversed(self, name=None):
884+
raise NotImplementedError()
885+
886+
861887
class Normalize(object):
862888
"""
863889
A class which, when called, can normalize data into
@@ -1390,8 +1416,8 @@ def __call__(self, values, clip=None):
13901416
if clip is None:
13911417
clip = [self.norm1.clip, self.norm2.clip]
13921418

1393-
return [self.norm1(values[0], clip=clip[0]),
1394-
self.norm2(values[1], clip=clip[1])]
1419+
return np.array([self.norm1(values[0], clip=clip[0]),
1420+
self.norm2(values[1], clip=clip[1])])
13951421

13961422
def inverse(self, values):
13971423
"""
@@ -1406,6 +1432,7 @@ def inverse(self, values):
14061432
"""
14071433
return [self.norm1.inverse(values[0]), self.norm.inverse(values[1])]
14081434

1435+
14091436
def rgb_to_hsv(arr):
14101437
"""
14111438
convert float rgb values (in the range [0, 1]), in a numpy array to hsv

0 commit comments

Comments
 (0)