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

Skip to content

Commit e41277c

Browse files
authored
Merge pull request #15479 from anntzer/axes_rgb
Cleanup axes_rgb.
2 parents ad9ba1e + 5e7ffda commit e41277c

4 files changed

Lines changed: 43 additions & 89 deletions

File tree

doc/api/next_api_changes/deprecations.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,10 @@ Support for passing ``None`` as base class to `.axes.subplot_class_factory`,
326326
``axes_grid1.parasite_axes.parasite_axes_class_factory``, and
327327
``axes_grid1.parasite_axes.parasite_axes_auxtrans_class_factory`` is deprecated.
328328
Explicitly pass the correct base ``Axes`` class instead.
329+
330+
``axes_rgb``
331+
~~~~~~~~~~~~
332+
In :mod:`mpl_toolkits.axes_grid1.axes_rgb`, ``imshow_rgb`` is deprecated (use
333+
``ax.imshow(np.dstack([r, g, b]))`` instead); ``RGBAxesBase`` is deprecated
334+
(use ``RGBAxes`` instead); ``RGBAxes.add_RGB_to_figure`` is deprecated (it was
335+
an internal helper).
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
from mpl_toolkits.axes_grid1.axes_rgb import (
2-
make_rgb_axes, imshow_rgb, RGBAxesBase)
3-
from mpl_toolkits.axisartist.axislines import Axes
4-
5-
6-
class RGBAxes(RGBAxesBase):
7-
_defaultAxesClass = Axes
1+
from mpl_toolkits.axisartist.axes_rgb import *
Lines changed: 33 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import numpy as np
22

3+
from matplotlib import cbook
34
from .axes_divider import make_axes_locatable, Size
45
from .mpl_axes import Axes
56

67

7-
def make_rgb_axes(ax, pad=0.01, axes_class=None, add_all=True):
8+
def make_rgb_axes(ax, pad=0.01, axes_class=None, add_all=True, **kwargs):
89
"""
910
Parameters
1011
----------
@@ -32,9 +33,8 @@ def make_rgb_axes(ax, pad=0.01, axes_class=None, add_all=True):
3233
axes_class = type(ax)
3334

3435
for ny in [4, 2, 0]:
35-
ax1 = axes_class(ax.get_figure(),
36-
ax.get_position(original=True),
37-
sharex=ax, sharey=ax)
36+
ax1 = axes_class(ax.get_figure(), ax.get_position(original=True),
37+
sharex=ax, sharey=ax, **kwargs)
3838
locator = divider.new_locator(nx=2, ny=ny)
3939
ax1.set_axes_locator(locator)
4040
for t in ax1.yaxis.get_ticklabels() + ax1.xaxis.get_ticklabels():
@@ -55,24 +55,14 @@ def make_rgb_axes(ax, pad=0.01, axes_class=None, add_all=True):
5555
return ax_rgb
5656

5757

58+
@cbook.deprecated("3.3", alternative="ax.imshow(np.dstack([r, g, b]))")
5859
def imshow_rgb(ax, r, g, b, **kwargs):
59-
ny, nx = r.shape
60-
R = np.zeros((ny, nx, 3))
61-
R[:, :, 0] = r
62-
G = np.zeros_like(R)
63-
G[:, :, 1] = g
64-
B = np.zeros_like(R)
65-
B[:, :, 2] = b
60+
return ax.imshow(np.dstack([r, g, b]), **kwargs)
6661

67-
RGB = R + G + B
6862

69-
im_rgb = ax.imshow(RGB, **kwargs)
70-
71-
return im_rgb
72-
73-
74-
class RGBAxesBase:
75-
"""base class for a 4-panel imshow (RGB, R, G, B)
63+
class RGBAxes:
64+
"""
65+
4-panel imshow (RGB, R, G, B).
7666
7767
Layout:
7868
+---------------+-----+
@@ -83,20 +73,22 @@ class RGBAxesBase:
8373
| | B |
8474
+---------------+-----+
8575
76+
Subclasses can override the ``_defaultAxesClass`` attribute.
77+
8678
Attributes
8779
----------
88-
_defaultAxesClass : matplotlib.axes.Axes
89-
defaults to 'Axes' in RGBAxes child class.
90-
No default in abstract base class
91-
RGB : _defaultAxesClass
92-
The axes object for the three-channel imshow
93-
R : _defaultAxesClass
94-
The axes object for the red channel imshow
95-
G : _defaultAxesClass
96-
The axes object for the green channel imshow
97-
B : _defaultAxesClass
98-
The axes object for the blue channel imshow
80+
RGB : ``_defaultAxesClass``
81+
The axes object for the three-channel imshow.
82+
R : ``_defaultAxesClass``
83+
The axes object for the red channel imshow.
84+
G : ``_defaultAxesClass``
85+
The axes object for the green channel imshow.
86+
B : ``_defaultAxesClass``
87+
The axes object for the blue channel imshow.
9988
"""
89+
90+
_defaultAxesClass = Axes
91+
10092
def __init__(self, *args, pad=0, add_all=True, **kwargs):
10193
"""
10294
Parameters
@@ -112,47 +104,12 @@ def __init__(self, *args, pad=0, add_all=True, **kwargs):
112104
**kwargs
113105
Unpacked into axes_class() init for RGB, R, G, B axes
114106
"""
115-
try:
116-
axes_class = kwargs.pop("axes_class", self._defaultAxesClass)
117-
except AttributeError as err:
118-
raise AttributeError(
119-
'A subclass of RGBAxesBase must have a _defaultAxesClass '
120-
'attribute. If you are not sure which axes class to use, '
121-
'consider using mpl_toolkits.axes_grid1.mpl_axes.Axes.'
122-
) from err
123-
124-
ax = axes_class(*args, **kwargs)
125-
126-
divider = make_axes_locatable(ax)
127-
128-
pad_size = pad * Size.AxesY(ax)
129-
130-
xsize = ((1-2*pad)/3) * Size.AxesX(ax)
131-
ysize = ((1-2*pad)/3) * Size.AxesY(ax)
132-
133-
divider.set_horizontal([Size.AxesX(ax), pad_size, xsize])
134-
divider.set_vertical([ysize, pad_size, ysize, pad_size, ysize])
135-
136-
ax.set_axes_locator(divider.new_locator(0, 0, ny1=-1))
137-
138-
ax_rgb = []
139-
for ny in [4, 2, 0]:
140-
ax1 = axes_class(ax.get_figure(),
141-
ax.get_position(original=True),
142-
sharex=ax, sharey=ax, **kwargs)
143-
locator = divider.new_locator(nx=2, ny=ny)
144-
ax1.set_axes_locator(locator)
145-
ax1.axis[:].toggle(ticklabels=False)
146-
ax_rgb.append(ax1)
147-
148-
self.RGB = ax
149-
self.R, self.G, self.B = ax_rgb
150-
107+
axes_class = kwargs.pop("axes_class", self._defaultAxesClass)
108+
self.RGB = ax = axes_class(*args, **kwargs)
151109
if add_all:
152-
fig = ax.get_figure()
153-
fig.add_axes(ax)
154-
self.add_RGB_to_figure()
155-
110+
ax.get_figure().add_axes(ax)
111+
self.R, self.G, self.B = make_rgb_axes(
112+
ax, pad=pad, axes_class=axes_class, add_all=add_all, **kwargs)
156113
self._config_axes()
157114

158115
def _config_axes(self, line_color='w', marker_edge_color='w'):
@@ -167,6 +124,7 @@ def _config_axes(self, line_color='w', marker_edge_color='w'):
167124
ax1.axis[:].line.set_color(line_color)
168125
ax1.axis[:].major_ticks.set_markeredgecolor(marker_edge_color)
169126

127+
@cbook.deprecated("3.3")
170128
def add_RGB_to_figure(self):
171129
"""Add the red, green and blue axes to the RGB composite's axes figure
172130
"""
@@ -196,26 +154,22 @@ def imshow_rgb(self, r, g, b, **kwargs):
196154
b : matplotlib.image.AxesImage
197155
"""
198156
if not (r.shape == g.shape == b.shape):
199-
raise ValueError('Input shapes do not match.'
200-
'\nr.shape = {}'
201-
'\ng.shape = {}'
202-
'\nb.shape = {}'
203-
.format(r.shape, g.shape, b.shape))
157+
raise ValueError(
158+
f'Input shapes ({r.shape}, {g.shape}, {b.shape}) do not match')
204159
RGB = np.dstack([r, g, b])
205160
R = np.zeros_like(RGB)
206161
R[:, :, 0] = r
207162
G = np.zeros_like(RGB)
208163
G[:, :, 1] = g
209164
B = np.zeros_like(RGB)
210165
B[:, :, 2] = b
211-
212166
im_rgb = self.RGB.imshow(RGB, **kwargs)
213167
im_r = self.R.imshow(R, **kwargs)
214168
im_g = self.G.imshow(G, **kwargs)
215169
im_b = self.B.imshow(B, **kwargs)
216-
217170
return im_rgb, im_r, im_g, im_b
218171

219172

220-
class RGBAxes(RGBAxesBase):
221-
_defaultAxesClass = Axes
173+
@cbook.deprecated("3.3", alternative="RGBAxes")
174+
class RGBAxesBase(RGBAxes):
175+
pass
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from mpl_toolkits.axes_grid1.axes_rgb import (
2-
make_rgb_axes, imshow_rgb, RGBAxesBase)
3-
2+
make_rgb_axes, imshow_rgb, RGBAxes as _RGBAxes)
43
from .axislines import Axes
54

65

7-
class RGBAxes(RGBAxesBase):
6+
class RGBAxes(_RGBAxes):
87
_defaultAxesClass = Axes

0 commit comments

Comments
 (0)