2
2
Helper classes to adjust the positions of multiple axes at drawing time.
3
3
"""
4
4
5
+ import functools
6
+
5
7
import numpy as np
6
8
7
9
import matplotlib as mpl
@@ -171,8 +173,45 @@ def get_position_runtime(self, ax, renderer):
171
173
else :
172
174
return self ._locator (ax , renderer ).bounds
173
175
176
+ def new_locator (self , nx , ny , nx1 = None , ny1 = None ):
177
+ """
178
+ Return an axes locator callable for the specified cell.
179
+
180
+ Parameters
181
+ ----------
182
+ nx, nx1 : int
183
+ Integers specifying the column-position of the
184
+ cell. When *nx1* is None, a single *nx*-th column is
185
+ specified. Otherwise, location of columns spanning between *nx*
186
+ to *nx1* (but excluding *nx1*-th column) is specified.
187
+ ny, ny1 : int
188
+ Same as *nx* and *nx1*, but for row positions.
189
+ """
190
+ if nx1 is None :
191
+ nx1 = nx + 1
192
+ if ny1 is None :
193
+ ny1 = ny + 1
194
+ # append_size("left") adds a new size at the beginning of the
195
+ # horizontal size lists; this shift transforms e.g.
196
+ # new_locator(nx=2, ...) into effectively new_locator(nx=3, ...). To
197
+ # take that into account, instead of recording nx, we record
198
+ # nx-self._xrefindex, where _xrefindex is shifted by 1 by each
199
+ # append_size("left"), and re-add self._xrefindex back to nx in
200
+ # _locate, when the actual axes position is computed. Ditto for y.
201
+ xref = self ._xrefindex
202
+ yref = self ._yrefindex
203
+ locator = functools .partial (
204
+ self ._locate , nx - xref , ny - yref , nx1 - xref , ny1 - yref )
205
+ locator .get_subplotspec = _api .deprecated (
206
+ "3.7" , alternative = "divider.get_subplotspec" )(self .get_subplotspec )
207
+ return locator
208
+
209
+ @_api .deprecated (
210
+ "3.7" , alternative = "divider.new_locator(...)(ax, renderer)" )
174
211
def locate (self , nx , ny , nx1 = None , ny1 = None , axes = None , renderer = None ):
175
212
"""
213
+ Implementation of ``divider.new_locator().__call__``.
214
+
176
215
Parameters
177
216
----------
178
217
nx, nx1 : int
@@ -185,6 +224,25 @@ def locate(self, nx, ny, nx1=None, ny1=None, axes=None, renderer=None):
185
224
axes
186
225
renderer
187
226
"""
227
+ xref = self ._xrefindex
228
+ yref = self ._yrefindex
229
+ return self ._locate (
230
+ nx - xref , (nx + 1 if nx1 is None else nx1 ) - xref ,
231
+ ny - yref , (ny + 1 if ny1 is None else ny1 ) - yref ,
232
+ axes , renderer )
233
+
234
+ def _locate (self , nx , ny , nx1 , ny1 , axes , renderer ):
235
+ """
236
+ Implementation of ``divider.new_locator().__call__``.
237
+
238
+ The axes locator callable returned by ``new_locator()`` is created as
239
+ a `functools.partial` of this method with *nx*, *ny*, *nx1*, and *ny1*
240
+ specifying the requested cell.
241
+ """
242
+ nx += self ._xrefindex
243
+ nx1 += self ._xrefindex
244
+ ny += self ._yrefindex
245
+ ny1 += self ._yrefindex
188
246
189
247
fig_w , fig_h = self ._fig .bbox .size / self ._fig .dpi
190
248
x , y , w , h = self .get_position_runtime (axes , renderer )
@@ -210,43 +268,11 @@ def locate(self, nx, ny, nx1=None, ny1=None, axes=None, renderer=None):
210
268
oy = self ._calc_offsets (vsizes , k_v )
211
269
x0 , y0 = x , y
212
270
213
- if nx1 is None :
214
- _api .warn_deprecated (
215
- "3.5" , message = "Support for passing nx1=None to mean nx+1 is "
216
- "deprecated since %(since)s; in a future version, nx1=None "
217
- "will mean 'up to the last cell'." )
218
- nx1 = nx + 1
219
- if ny1 is None :
220
- _api .warn_deprecated (
221
- "3.5" , message = "Support for passing ny1=None to mean ny+1 is "
222
- "deprecated since %(since)s; in a future version, ny1=None "
223
- "will mean 'up to the last cell'." )
224
- ny1 = ny + 1
225
-
226
271
x1 , w1 = x0 + ox [nx ] / fig_w , (ox [nx1 ] - ox [nx ]) / fig_w
227
272
y1 , h1 = y0 + oy [ny ] / fig_h , (oy [ny1 ] - oy [ny ]) / fig_h
228
273
229
274
return mtransforms .Bbox .from_bounds (x1 , y1 , w1 , h1 )
230
275
231
- def new_locator (self , nx , ny , nx1 = None , ny1 = None ):
232
- """
233
- Return a new `.AxesLocator` for the specified cell.
234
-
235
- Parameters
236
- ----------
237
- nx, nx1 : int
238
- Integers specifying the column-position of the
239
- cell. When *nx1* is None, a single *nx*-th column is
240
- specified. Otherwise, location of columns spanning between *nx*
241
- to *nx1* (but excluding *nx1*-th column) is specified.
242
- ny, ny1 : int
243
- Same as *nx* and *nx1*, but for row positions.
244
- """
245
- return AxesLocator (
246
- self , nx , ny ,
247
- nx1 if nx1 is not None else nx + 1 ,
248
- ny1 if ny1 is not None else ny + 1 )
249
-
250
276
def append_size (self , position , size ):
251
277
_api .check_in_list (["left" , "right" , "bottom" , "top" ],
252
278
position = position )
@@ -281,6 +307,7 @@ def add_auto_adjustable_area(self, use_axes, pad=0.1, adjust_dirs=None):
281
307
self .append_size (d , Size ._AxesDecorationsSize (use_axes , d ) + pad )
282
308
283
309
310
+ @_api .deprecated ("3.7" )
284
311
class AxesLocator :
285
312
"""
286
313
A callable object which returns the position and size of a given
@@ -308,16 +335,8 @@ def __init__(self, axes_divider, nx, ny, nx1=None, ny1=None):
308
335
self ._nx , self ._ny = nx - _xrefindex , ny - _yrefindex
309
336
310
337
if nx1 is None :
311
- _api .warn_deprecated (
312
- "3.5" , message = "Support for passing nx1=None to mean nx+1 is "
313
- "deprecated since %(since)s; in a future version, nx1=None "
314
- "will mean 'up to the last cell'." )
315
338
nx1 = nx + 1
316
339
if ny1 is None :
317
- _api .warn_deprecated (
318
- "3.5" , message = "Support for passing ny1=None to mean ny+1 is "
319
- "deprecated since %(since)s; in a future version, ny1=None "
320
- "will mean 'up to the last cell'." )
321
340
ny1 = ny + 1
322
341
323
342
self ._nx1 = nx1 - _xrefindex
@@ -425,24 +444,17 @@ def new_horizontal(self, size, pad=None, pack_start=False, **kwargs):
425
444
"""
426
445
if pad is None :
427
446
pad = mpl .rcParams ["figure.subplot.wspace" ] * self ._xref
447
+ pos = "left" if pack_start else "right"
428
448
if pad :
429
449
if not isinstance (pad , Size ._Base ):
430
450
pad = Size .from_any (pad , fraction_ref = self ._xref )
431
- if pack_start :
432
- self ._horizontal .insert (0 , pad )
433
- self ._xrefindex += 1
434
- else :
435
- self ._horizontal .append (pad )
451
+ self .append_size (pos , pad )
436
452
if not isinstance (size , Size ._Base ):
437
453
size = Size .from_any (size , fraction_ref = self ._xref )
438
- if pack_start :
439
- self ._horizontal .insert (0 , size )
440
- self ._xrefindex += 1
441
- locator = self .new_locator (nx = 0 , ny = self ._yrefindex )
442
- else :
443
- self ._horizontal .append (size )
444
- locator = self .new_locator (
445
- nx = len (self ._horizontal ) - 1 , ny = self ._yrefindex )
454
+ self .append_size (pos , size )
455
+ locator = self .new_locator (
456
+ nx = 0 if pack_start else len (self ._horizontal ) - 1 ,
457
+ ny = self ._yrefindex )
446
458
ax = self ._get_new_axes (** kwargs )
447
459
ax .set_axes_locator (locator )
448
460
return ax
@@ -457,24 +469,17 @@ def new_vertical(self, size, pad=None, pack_start=False, **kwargs):
457
469
"""
458
470
if pad is None :
459
471
pad = mpl .rcParams ["figure.subplot.hspace" ] * self ._yref
472
+ pos = "bottom" if pack_start else "top"
460
473
if pad :
461
474
if not isinstance (pad , Size ._Base ):
462
475
pad = Size .from_any (pad , fraction_ref = self ._yref )
463
- if pack_start :
464
- self ._vertical .insert (0 , pad )
465
- self ._yrefindex += 1
466
- else :
467
- self ._vertical .append (pad )
476
+ self .append_size (pos , pad )
468
477
if not isinstance (size , Size ._Base ):
469
478
size = Size .from_any (size , fraction_ref = self ._yref )
470
- if pack_start :
471
- self ._vertical .insert (0 , size )
472
- self ._yrefindex += 1
473
- locator = self .new_locator (nx = self ._xrefindex , ny = 0 )
474
- else :
475
- self ._vertical .append (size )
476
- locator = self .new_locator (
477
- nx = self ._xrefindex , ny = len (self ._vertical ) - 1 )
479
+ self .append_size (pos , size )
480
+ locator = self .new_locator (
481
+ nx = self ._xrefindex ,
482
+ ny = 0 if pack_start else len (self ._vertical ) - 1 )
478
483
ax = self ._get_new_axes (** kwargs )
479
484
ax .set_axes_locator (locator )
480
485
return ax
@@ -603,7 +608,7 @@ class HBoxDivider(SubplotDivider):
603
608
604
609
def new_locator (self , nx , nx1 = None ):
605
610
"""
606
- Create a new `.AxesLocator` for the specified cell.
611
+ Create an axes locator callable for the specified cell.
607
612
608
613
Parameters
609
614
----------
@@ -613,22 +618,18 @@ def new_locator(self, nx, nx1=None):
613
618
specified. Otherwise, location of columns spanning between *nx*
614
619
to *nx1* (but excluding *nx1*-th column) is specified.
615
620
"""
616
- return AxesLocator ( self , nx , 0 , nx1 if nx1 is not None else nx + 1 , 1 )
621
+ return super (). new_locator ( nx , 0 , nx1 , 0 )
617
622
618
- def locate (self , nx , ny , nx1 = None , ny1 = None , axes = None , renderer = None ):
623
+ def _locate (self , nx , ny , nx1 , ny1 , axes , renderer ):
619
624
# docstring inherited
625
+ nx += self ._xrefindex
626
+ nx1 += self ._xrefindex
620
627
fig_w , fig_h = self ._fig .bbox .size / self ._fig .dpi
621
628
x , y , w , h = self .get_position_runtime (axes , renderer )
622
629
summed_ws = self .get_horizontal_sizes (renderer )
623
630
equal_hs = self .get_vertical_sizes (renderer )
624
631
x0 , y0 , ox , hh = _locate (
625
632
x , y , w , h , summed_ws , equal_hs , fig_w , fig_h , self .get_anchor ())
626
- if nx1 is None :
627
- _api .warn_deprecated (
628
- "3.5" , message = "Support for passing nx1=None to mean nx+1 is "
629
- "deprecated since %(since)s; in a future version, nx1=None "
630
- "will mean 'up to the last cell'." )
631
- nx1 = nx + 1
632
633
x1 , w1 = x0 + ox [nx ] / fig_w , (ox [nx1 ] - ox [nx ]) / fig_w
633
634
y1 , h1 = y0 , hh
634
635
return mtransforms .Bbox .from_bounds (x1 , y1 , w1 , h1 )
@@ -642,7 +643,7 @@ class VBoxDivider(SubplotDivider):
642
643
643
644
def new_locator (self , ny , ny1 = None ):
644
645
"""
645
- Create a new `.AxesLocator` for the specified cell.
646
+ Create an axes locator callable for the specified cell.
646
647
647
648
Parameters
648
649
----------
@@ -652,22 +653,18 @@ def new_locator(self, ny, ny1=None):
652
653
specified. Otherwise, location of rows spanning between *ny*
653
654
to *ny1* (but excluding *ny1*-th row) is specified.
654
655
"""
655
- return AxesLocator ( self , 0 , ny , 1 , ny1 if ny1 is not None else ny + 1 )
656
+ return super (). new_locator ( 0 , ny , 0 , ny1 )
656
657
657
- def locate (self , nx , ny , nx1 = None , ny1 = None , axes = None , renderer = None ):
658
+ def _locate (self , nx , ny , nx1 , ny1 , axes , renderer ):
658
659
# docstring inherited
660
+ ny += self ._yrefindex
661
+ ny1 += self ._yrefindex
659
662
fig_w , fig_h = self ._fig .bbox .size / self ._fig .dpi
660
663
x , y , w , h = self .get_position_runtime (axes , renderer )
661
664
summed_hs = self .get_vertical_sizes (renderer )
662
665
equal_ws = self .get_horizontal_sizes (renderer )
663
666
y0 , x0 , oy , ww = _locate (
664
667
y , x , h , w , summed_hs , equal_ws , fig_h , fig_w , self .get_anchor ())
665
- if ny1 is None :
666
- _api .warn_deprecated (
667
- "3.5", message = "Support for passing ny1=None to mean ny+1 is "
668
- "deprecated since %(since)s; in a future version, ny1=None "
669
- "will mean 'up to the last cell'." )
670
- ny1 = ny + 1
671
668
x1 , w1 = x0 , ww
672
669
y1 , h1 = y0 + oy [ny ] / fig_h , (oy [ny1 ] - oy [ny ]) / fig_h
673
670
return mtransforms .Bbox .from_bounds (x1 , y1 , w1 , h1 )
0 commit comments