11import numpy as np
22
3+ from matplotlib import cbook
34from .axes_divider import make_axes_locatable , Size
45from .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]))" )
5859def 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- '\n r.shape = {}'
201- '\n g.shape = {}'
202- '\n b.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
0 commit comments