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,47 @@ 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" , name = "get_subplotspec" ,
207
+ alternative = "divider.get_subplotspec" )(
208
+ lambda : self .get_subplotspec ())
209
+ return locator
210
+
211
+ @_api .deprecated (
212
+ "3.7" , alternative = "divider.new_locator(...)(ax, renderer)" )
174
213
def locate (self , nx , ny , nx1 = None , ny1 = None , axes = None , renderer = None ):
175
214
"""
215
+ Implementation of ``divider.new_locator().__call__``.
216
+
176
217
Parameters
177
218
----------
178
219
nx, nx1 : int
@@ -185,6 +226,25 @@ def locate(self, nx, ny, nx1=None, ny1=None, axes=None, renderer=None):
185
226
axes
186
227
renderer
187
228
"""
229
+ xref = self ._xrefindex
230
+ yref = self ._yrefindex
231
+ return self ._locate (
232
+ nx - xref , (nx + 1 if nx1 is None else nx1 ) - xref ,
233
+ ny - yref , (ny + 1 if ny1 is None else ny1 ) - yref ,
234
+ axes , renderer )
235
+
236
+ def _locate (self , nx , ny , nx1 , ny1 , axes , renderer ):
237
+ """
238
+ Implementation of ``divider.new_locator().__call__``.
239
+
240
+ The axes locator callable returned by ``new_locator()`` is created as
241
+ a `functools.partial` of this method with *nx*, *ny*, *nx1*, and *ny1*
242
+ specifying the requested cell.
243
+ """
244
+ nx += self ._xrefindex
245
+ nx1 += self ._xrefindex
246
+ ny += self ._yrefindex
247
+ ny1 += self ._yrefindex
188
248
189
249
fig_w , fig_h = self ._fig .bbox .size / self ._fig .dpi
190
250
x , y , w , h = self .get_position_runtime (axes , renderer )
@@ -210,43 +270,11 @@ def locate(self, nx, ny, nx1=None, ny1=None, axes=None, renderer=None):
210
270
oy = self ._calc_offsets (vsizes , k_v )
211
271
x0 , y0 = x , y
212
272
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
273
x1 , w1 = x0 + ox [nx ] / fig_w , (ox [nx1 ] - ox [nx ]) / fig_w
227
274
y1 , h1 = y0 + oy [ny ] / fig_h , (oy [ny1 ] - oy [ny ]) / fig_h
228
275
229
276
return mtransforms .Bbox .from_bounds (x1 , y1 , w1 , h1 )
230
277
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
278
def append_size (self , position , size ):
251
279
_api .check_in_list (["left" , "right" , "bottom" , "top" ],
252
280
position = position )
@@ -281,6 +309,7 @@ def add_auto_adjustable_area(self, use_axes, pad=0.1, adjust_dirs=None):
281
309
self .append_size (d , Size ._AxesDecorationsSize (use_axes , d ) + pad )
282
310
283
311
312
+ @_api .deprecated ("3.7" )
284
313
class AxesLocator :
285
314
"""
286
315
A callable object which returns the position and size of a given
@@ -308,16 +337,8 @@ def __init__(self, axes_divider, nx, ny, nx1=None, ny1=None):
308
337
self ._nx , self ._ny = nx - _xrefindex , ny - _yrefindex
309
338
310
339
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
340
nx1 = nx + 1
316
341
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
342
ny1 = ny + 1
322
343
323
344
self ._nx1 = nx1 - _xrefindex
@@ -425,24 +446,17 @@ def new_horizontal(self, size, pad=None, pack_start=False, **kwargs):
425
446
"""
426
447
if pad is None :
427
448
pad = mpl .rcParams ["figure.subplot.wspace" ] * self ._xref
449
+ pos = "left" if pack_start else "right"
428
450
if pad :
429
451
if not isinstance (pad , Size ._Base ):
430
452
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 )
453
+ self .append_size (pos , pad )
436
454
if not isinstance (size , Size ._Base ):
437
455
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 )
456
+ self .append_size (pos , size )
457
+ locator = self .new_locator (
458
+ nx = 0 if pack_start else len (self ._horizontal ) - 1 ,
459
+ ny = self ._yrefindex )
446
460
ax = self ._get_new_axes (** kwargs )
447
461
ax .set_axes_locator (locator )
448
462
return ax
@@ -457,24 +471,17 @@ def new_vertical(self, size, pad=None, pack_start=False, **kwargs):
457
471
"""
458
472
if pad is None :
459
473
pad
2851
span> = mpl .rcParams ["figure.subplot.hspace" ] * self ._yref
474
+ pos = "bottom" if pack_start else "top"
460
475
if pad :
461
476
if not isinstance (pad , Size ._Base ):
462
477
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 )
478
+ self .append_size (pos , pad )
468
479
if not isinstance (size , Size ._Base ):
469
480
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 )
481
+ self .append_size (pos , size )
482
+ locator = self .new_locator (
483
+ nx = self ._xrefindex ,
484
+ ny = 0 if pack_start else len (self ._vertical ) - 1 )
478
485
ax = self ._get_new_axes (** kwargs )
479
486
ax .set_axes_locator (locator )
480
487
return ax
@@ -603,7 +610,7 @@ class HBoxDivider(SubplotDivider):
603
610
604
611
def new_locator (self , nx , nx1 = None ):
605
612
"""
606
- Create a new `.AxesLocator` for the specified cell.
613
+ Create an axes locator callable for the specified cell.
607
614
608
615
Parameters
609
616
----------
@@ -613,22 +620,18 @@ def new_locator(self, nx, nx1=None):
613
620
specified. Otherwise, location of columns spanning between *nx*
614
621
to *nx1* (but excluding *nx1*-th column) is specified.
615
622
"""
616
- return AxesLocator ( self , nx , 0 , nx1 if nx1 is not None else nx + 1 , 1 )
623
+ return super (). new_locator ( nx , 0 , nx1 , 0 )
617
624
618
- def locate (self , nx , ny , nx1 = None , ny1 = None , axes = None , renderer = None ):
625
+ def _locate (self , nx , ny , nx1 , ny1 , axes , renderer ):
619
626
# docstring inherited
627
+ nx += self ._xrefindex
628
+ nx1 += self ._xrefindex
620
629
fig_w , fig_h = self ._fig .bbox .size / self ._fig .dpi
621
630
x , y , w , h = self .get_position_runtime (axes , renderer )
622
631
summed_ws = self .get_horizontal_sizes (renderer )
623
632
equal_hs = self .get_vertical_sizes (renderer )
624
633
x0 , y0 , ox , hh = _locate (
625
634
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
635
x1 , w1 = x0 + ox [nx ] / fig_w , (ox [nx1 ] - ox [nx ]) / fig_w
633
636
y1 , h1 = y0 , hh
634
637
return mtransforms .Bbox .from_bounds (x1 , y1 , w1 , h1 )
@@ -642,7 +645,7 @@ class VBoxDivider(SubplotDivider):
642
645
643
646
def new_locator (self , ny , ny1 = None ):
644
647
"""
645
- Create a new `.AxesLocator` for the specified cell.
648
+ Create an axes locator callable for the specified cell.
646
649
647
650
Parameters
648
651
----------
@@ -652,22 +655,18 @@ def new_locator(self, ny, ny1=None):
652
655
specified. Otherwise, location of rows spanning between *ny*
653
656
to *ny1* (but excluding *ny1*-th row) is specified.
654
657
"""
655
- return AxesLocator ( self , 0 , ny , 1 , ny1 if ny1 is not None else ny + 1 )
658
+ return super (). new_locator ( 0 , ny , 0 , ny1 )
656
659
657
- def locate (self , nx , ny , nx1 = None , ny1 = None , axes = None , renderer = None ):
660
+ def _locate (self , nx , ny , nx1 , ny1 , axes , renderer ):
658
661
# docstring inherited
662
+ ny += self ._yrefindex
663
+ ny1 += self ._yrefindex
659
664
fig_w , fig_h = self ._fig .bbox .size / self ._fig .dpi
660
665
x , y , w , h = self .get_position_runtime (axes , renderer )
661
666
summed_hs = self .get_vertical_sizes (renderer )
662
667
equal_ws = self .get_horizontal_sizes (renderer )
663
668
y0 , x0 , oy , ww = _locate (
664
669
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
670
x1 , w1 = x0 , ww
672
671
y1 , h1 = y0 + oy [ny ] / fig_h , (oy [ny1 ] - oy [ny ]) / fig_h
673
672
return mtransforms .Bbox .from_bounds (x1 , y1 , w1 , h1 )
0 commit comments