Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9c0a646

Browse files
committed
Fix ngrids support in axes_grid.Grid().
This also restores axes_row and axes_column to their behavior prior to88e0d84(which is the commit that broke ngrids support).
1 parent177f28c commit9c0a646

File tree

3 files changed

+48
-31
lines changed

3 files changed

+48
-31
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
``axes_grid.Grid.ngrids``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~
3+
This attribute has been deprecated and renamed ``n_axes``, consistently with
4+
the new name of the `~.axes_grid.Grid` constructor parameter that allows
5+
setting the actual number of axes in the grid (the old parameter, ``ngrids``,
6+
did not actually work since Matplotlib 3.3).

‎lib/mpl_toolkits/axes_grid1/axes_grid.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ class Grid:
5757

5858
_defaultAxesClass=Axes
5959

60+
@_api.rename_parameter("3.11","ngrids","n_axes")
6061
def__init__(self,fig,
6162
rect,
6263
nrows_ncols,
63-
ngrids=None,
64+
n_axes=None,
6465
direction="row",
6566
axes_pad=0.02,
6667
*,
@@ -83,8 +84,8 @@ def __init__(self, fig,
8384
``121``), or as a `~.SubplotSpec`.
8485
nrows_ncols : (int, int)
8586
Number of rows and columns in the grid.
86-
ngrids : int or None, default: None
87-
If not None, only the first *ngrids* axes in the grid are created.
87+
n_axes : int or None, default: None
88+
If not None, only the first *n_axes* axes in the grid are created.
8889
direction : {"row", "column"}, default: "row"
8990
Whether axes are created in row-major ("row by row") or
9091
column-major order ("column by column"). This also affects the
@@ -116,14 +117,14 @@ def __init__(self, fig,
116117
"""
117118
self._nrows,self._ncols=nrows_ncols
118119

119-
ifngridsisNone:
120-
ngrids=self._nrows*self._ncols
120+
ifn_axesisNone:
121+
n_axes=self._nrows*self._ncols
121122
else:
122-
ifnot0<ngrids<=self._nrows*self._ncols:
123+
ifnot0<n_axes<=self._nrows*self._ncols:
123124
raiseValueError(
124-
"ngrids must be positive and not larger than nrows*ncols")
125+
"n_axes must be positive and not larger than nrows*ncols")
125126

126-
self.ngrids=ngrids
127+
self.n_axes=n_axes
127128

128129
self._horiz_pad_size,self._vert_pad_size=map(
129130
Size.Fixed,np.broadcast_to(axes_pad,2))
@@ -150,7 +151,7 @@ def __init__(self, fig,
150151
rect=self._divider.get_position()
151152

152153
axes_array=np.full((self._nrows,self._ncols),None,dtype=object)
153-
foriinrange(self.ngrids):
154+
foriinrange(self.n_axes):
154155
col,row=self._get_col_row(i)
155156
ifshare_all:
156157
sharex=sharey=axes_array[0,0]
@@ -160,9 +161,9 @@ def __init__(self, fig,
160161
axes_array[row,col]=axes_class(
161162
fig,rect,sharex=sharex,sharey=sharey)
162163
self.axes_all=axes_array.ravel(
163-
order="C"ifself._direction=="row"else"F").tolist()
164-
self.axes_column=axes_array.T.tolist()
165-
self.axes_row=axes_array.tolist()
164+
order="C"ifself._direction=="row"else"F").tolist()[:n_axes]
165+
self.axes_row=[[axforaxinrowifax]forrowinaxes_array]
166+
self.axes_column=[[axforaxincolifax]forcolinaxes_array.T]
166167
self.axes_llc=self.axes_column[0][-1]
167168

168169
self._init_locators()
@@ -177,7 +178,7 @@ def _init_locators(self):
177178
[Size.Scaled(1),self._horiz_pad_size]* (self._ncols-1)+ [Size.Scaled(1)])
178179
self._divider.set_vertical(
179180
[Size.Scaled(1),self._vert_pad_size]* (self._nrows-1)+ [Size.Scaled(1)])
180-
foriinrange(self.ngrids):
181+
foriinrange(self.n_axes):
181182
col,row=self._get_col_row(i)
182183
self.axes_all[i].set_axes_locator(
183184
self._divider.new_locator(nx=2*col,ny=2* (self._nrows-1-row)))
@@ -190,6 +191,8 @@ def _get_col_row(self, n):
190191

191192
returncol,row
192193

194+
ngrids=_api.deprecated(property(lambdaself:self.n_axes))
195+
193196
# Good to propagate __len__ if we have __getitem__
194197
def__len__(self):
195198
returnlen(self.axes_all)
@@ -254,7 +257,10 @@ def set_label_mode(self, mode):
254257
ifmode=="keep":
255258
return
256259
fori,jinnp.ndindex(self._nrows,self._ncols):
257-
ax=self.axes_row[i][j]
260+
try:
261+
ax=self.axes_row[i][j]
262+
exceptIndexError:
263+
continue
258264
ifisinstance(ax.axis,MethodType):
259265
bottom_axis=SimpleAxisArtist(ax.xaxis,1,ax.spines["bottom"])
260266
left_axis=SimpleAxisArtist(ax.yaxis,1,ax.spines["left"])
@@ -293,7 +299,7 @@ class ImageGrid(Grid):
293299
def__init__(self,fig,
294300
rect,
295301
nrows_ncols,
296-
ngrids=None,
302+
n_axes=None,
297303
direction="row",
298304
axes_pad=0.02,
299305
*,
@@ -317,8 +323,8 @@ def __init__(self, fig,
317323
as a three-digit subplot position code (e.g., "121").
318324
nrows_ncols : (int, int)
319325
Number of rows and columns in the grid.
320-
ngrids : int or None, default: None
321-
If not None, only the first *ngrids* axes in the grid are created.
326+
n_axes : int or None, default: None
327+
If not None, only the first *n_axes* axes in the grid are created.
322328
direction : {"row", "column"}, default: "row"
323329
Whether axes are created in row-major ("row by row") or
324330
column-major order ("column by column"). This also affects the
@@ -372,7 +378,7 @@ def __init__(self, fig,
372378
# The colorbar axes are created in _init_locators().
373379

374380
super().__init__(
375-
fig,rect,nrows_ncols,ngrids,
381+
fig,rect,nrows_ncols,n_axes,
376382
direction=direction,axes_pad=axes_pad,
377383
share_all=share_all,share_x=True,share_y=True,aspect=aspect,
378384
label_mode=label_mode,axes_class=axes_class)
@@ -408,7 +414,7 @@ def _init_locators(self):
408414
_cbaraxes_class_factory(self._defaultAxesClass)(
409415
self.axes_all[0].get_figure(root=False),self._divider.get_position(),
410416
orientation=self._colorbar_location)
411-
for_inrange(self.ngrids)]
417+
for_inrange(self.n_axes)]
412418

413419
cb_mode=self._colorbar_mode
414420
cb_location=self._colorbar_location
@@ -429,7 +435,7 @@ def _init_locators(self):
429435
v.append(Size.from_any(self._colorbar_size,sz))
430436
v.append(Size.from_any(self._colorbar_pad,sz))
431437
locator=self._divider.new_locator(nx=0,nx1=-1,ny=0)
432-
foriinrange(self.ngrids):
438+
foriinrange(self.n_axes):
433439
self.cbar_axes[i].set_visible(False)
434440
self.cbar_axes[0].set_axes_locator(locator)
435441
self.cbar_axes[0].set_visible(True)
@@ -490,7 +496,7 @@ def _init_locators(self):
490496
v_cb_pos.append(len(v))
491497
v.append(Size.from_any(self._colorbar_size,sz))
492498

493-
foriinrange(self.ngrids):
499+
foriinrange(self.n_axes):
494500
col,row=self._get_col_row(i)
495501
locator=self._divider.new_locator(nx=h_ax_pos[col],
496502
ny=v_ax_pos[self._nrows-1-row])
@@ -530,12 +536,12 @@ def _init_locators(self):
530536
v.append(Size.from_any(self._colorbar_size,sz))
531537
locator=self._divider.new_locator(nx=0,nx1=-1,ny=-2)
532538
ifcb_locationin ("right","top"):
533-
foriinrange(self.ngrids):
539+
foriinrange(self.n_axes):
534540
self.cbar_axes[i].set_visible(False)
535541
self.cbar_axes[0].set_axes_locator(locator)
536542
self.cbar_axes[0].set_visible(True)
537543
elifcb_mode=="each":
538-
foriinrange(self.ngrids):
544+
foriinrange(self.n_axes):
539545
self.cbar_axes[i].set_visible(True)
540546
elifcb_mode=="edge":
541547
ifcb_locationin ("right","left"):
@@ -544,10 +550,10 @@ def _init_locators(self):
544550
count=self._ncols
545551
foriinrange(count):
546552
self.cbar_axes[i].set_visible(True)
547-
forjinrange(i+1,self.ngrids):
553+
forjinrange(i+1,self.n_axes):
548554
self.cbar_axes[j].set_visible(False)
549555
else:
550-
foriinrange(self.ngrids):
556+
foriinrange(self.n_axes):
551557
self.cbar_axes[i].set_visible(False)
552558
self.cbar_axes[i].set_position([1.,1.,0.001,0.001],
553559
which="active")

‎lib/mpl_toolkits/axes_grid1/tests/test_axes_grid1.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ def test_axesgrid_colorbar_log_smoketest():
104104
fig=plt.figure()
105105
grid=AxesGrid(fig,111,# modified to be only subplot
106106
nrows_ncols=(1,1),
107-
ngrids=1,
108107
label_mode="L",
109108
cbar_location="top",
110109
cbar_mode="single",
@@ -638,15 +637,15 @@ def test_grid_axes_position(direction):
638637
assertloc[3].args[1]==loc[2].args[1]
639638

640639

641-
@pytest.mark.parametrize('rect,ngrids, error, message', (
640+
@pytest.mark.parametrize('rect,n_axes, error, message', (
642641
((1,1),None,TypeError,"Incorrect rect format"),
643-
(111,-1,ValueError,"ngrids must be positive"),
644-
(111,7,ValueError,"ngrids must be positive"),
642+
(111,-1,ValueError,"n_axes must be positive"),
643+
(111,7,ValueError,"n_axes must be positive"),
645644
))
646-
deftest_grid_errors(rect,ngrids,error,message):
645+
deftest_grid_errors(rect,n_axes,error,message):
647646
fig=plt.figure()
648647
withpytest.raises(error,match=message):
649-
Grid(fig,rect, (2,3),ngrids=ngrids)
648+
Grid(fig,rect, (2,3),n_axes=n_axes)
650649

651650

652651
@pytest.mark.parametrize('anchor, error, message', (
@@ -780,3 +779,9 @@ def test_anchored_locator_base_call():
780779
deftest_grid_with_axes_class_not_overriding_axis():
781780
Grid(plt.figure(),111, (2,2),axes_class=mpl.axes.Axes)
782781
RGBAxes(plt.figure(),111,axes_class=mpl.axes.Axes)
782+
783+
784+
deftest_grid_n_axes():
785+
fig=plt.figure()
786+
grid=Grid(fig,111, (3,3),n_axes=5)
787+
assertlen(fig.axes)==grid.n_axes==5

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp