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

Fix ngrids support in axes_grid.Grid().#27972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
timhoffm merged 1 commit intomatplotlib:mainfromanntzer:gn
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletionsdoc/api/next_api_changes/deprecations/27972-AL.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
``axes_grid.Grid.ngrids``
~~~~~~~~~~~~~~~~~~~~~~~~~
This attribute has been deprecated and renamed ``n_axes``, consistently with
the new name of the `~.axes_grid.Grid` constructor parameter that allows
setting the actual number of axes in the grid (the old parameter, ``ngrids``,
did not actually work since Matplotlib 3.3).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
did not actually work since Matplotlib 3.3).
did not actually work since Matplotlib 3.3).
The same change has been made in ``axes_grid.ImageGrid``.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

done


The same change has been made in ``axes_grid.ImageGrid``.
59 changes: 32 additions & 27 deletionslib/mpl_toolkits/axes_grid1/axes_grid.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,16 +51,17 @@ class Grid:
in the usage pattern ``grid.axes_row[row][col]``.
axes_llc : Axes
The Axes in the lower left corner.
ngrids : int
n_axes : int
Number of Axes in the grid.
"""

_defaultAxesClass = Axes

@_api.rename_parameter("3.11", "ngrids", "n_axes")
def __init__(self, fig,
rect,
nrows_ncols,
ngrids=None,
n_axes=None,
direction="row",
axes_pad=0.02,
*,
Expand All@@ -83,8 +84,8 @@ def __init__(self, fig,
``121``), or as a `~.SubplotSpec`.
nrows_ncols : (int, int)
Number of rows and columns in the grid.
ngrids : int or None, default: None
If not None, only the first *ngrids* axes in the grid are created.
n_axes : int or None, default: None
If not None, only the first *n_axes* axes in the grid are created.
direction : {"row", "column"}, default: "row"
Whether axes are created in row-major ("row by row") or
column-major order ("column by column"). This also affects the
Expand DownExpand Up@@ -116,14 +117,12 @@ def __init__(self, fig,
"""
self._nrows, self._ncols = nrows_ncols

ifngrids is None:
ngrids = self._nrows * self._ncols
ifn_axes is None:
n_axes = self._nrows * self._ncols
else:
if not 0 <ngrids <= self._nrows * self._ncols:
if not 0 <n_axes <= self._nrows * self._ncols:
raise ValueError(
"ngrids must be positive and not larger than nrows*ncols")

self.ngrids = ngrids
"n_axes must be positive and not larger than nrows*ncols")

self._horiz_pad_size, self._vert_pad_size = map(
Size.Fixed, np.broadcast_to(axes_pad, 2))
Expand All@@ -150,7 +149,7 @@ def __init__(self, fig,
rect = self._divider.get_position()

axes_array = np.full((self._nrows, self._ncols), None, dtype=object)
for i in range(self.ngrids):
for i in range(n_axes):
col, row = self._get_col_row(i)
if share_all:
sharex = sharey = axes_array[0, 0]
Expand All@@ -160,9 +159,9 @@ def __init__(self, fig,
axes_array[row, col] = axes_class(
fig, rect, sharex=sharex, sharey=sharey)
self.axes_all = axes_array.ravel(
order="C" if self._direction == "row" else "F").tolist()
self.axes_column = axes_array.T.tolist()
self.axes_row = axes_array.tolist()
order="C" if self._direction == "row" else "F").tolist()[:n_axes]
self.axes_row =[[ax for ax in row if ax] for row inaxes_array]
self.axes_column =[[ax for ax in col if ax] for col inaxes_array.T]
self.axes_llc = self.axes_column[0][-1]

self._init_locators()
Expand All@@ -177,7 +176,7 @@ def _init_locators(self):
[Size.Scaled(1), self._horiz_pad_size] * (self._ncols-1) + [Size.Scaled(1)])
self._divider.set_vertical(
[Size.Scaled(1), self._vert_pad_size] * (self._nrows-1) + [Size.Scaled(1)])
for i in range(self.ngrids):
for i in range(self.n_axes):
col, row = self._get_col_row(i)
self.axes_all[i].set_axes_locator(
self._divider.new_locator(nx=2 * col, ny=2 * (self._nrows - 1 - row)))
Expand All@@ -190,6 +189,9 @@ def _get_col_row(self, n):

return col, row

n_axes = property(lambda self: len(self.axes_all))
ngrids = _api.deprecated(property(lambda self: len(self.axes_all)))

# Good to propagate __len__ if we have __getitem__
def __len__(self):
return len(self.axes_all)
Expand DownExpand Up@@ -254,7 +256,10 @@ def set_label_mode(self, mode):
if mode == "keep":
return
for i, j in np.ndindex(self._nrows, self._ncols):
ax = self.axes_row[i][j]
try:
ax = self.axes_row[i][j]
except IndexError:
continue
if isinstance(ax.axis, MethodType):
bottom_axis = SimpleAxisArtist(ax.xaxis, 1, ax.spines["bottom"])
left_axis = SimpleAxisArtist(ax.yaxis, 1, ax.spines["left"])
Expand DownExpand Up@@ -293,7 +298,7 @@ class ImageGrid(Grid):
def __init__(self, fig,
rect,
nrows_ncols,
ngrids=None,
n_axes=None,
direction="row",
axes_pad=0.02,
*,
Expand All@@ -317,8 +322,8 @@ def __init__(self, fig,
as a three-digit subplot position code (e.g., "121").
nrows_ncols : (int, int)
Number of rows and columns in the grid.
ngrids : int or None, default: None
If not None, only the first *ngrids* axes in the grid are created.
n_axes : int or None, default: None
If not None, only the first *n_axes* axes in the grid are created.
direction : {"row", "column"}, default: "row"
Whether axes are created in row-major ("row by row") or
column-major order ("column by column"). This also affects the
Expand DownExpand Up@@ -372,7 +377,7 @@ def __init__(self, fig,
# The colorbar axes are created in _init_locators().

super().__init__(
fig, rect, nrows_ncols,ngrids,
fig, rect, nrows_ncols,n_axes,
direction=direction, axes_pad=axes_pad,
share_all=share_all, share_x=True, share_y=True, aspect=aspect,
label_mode=label_mode, axes_class=axes_class)
Expand DownExpand Up@@ -408,7 +413,7 @@ def _init_locators(self):
_cbaraxes_class_factory(self._defaultAxesClass)(
self.axes_all[0].get_figure(root=False), self._divider.get_position(),
orientation=self._colorbar_location)
for _ in range(self.ngrids)]
for _ in range(self.n_axes)]

cb_mode = self._colorbar_mode
cb_location = self._colorbar_location
Expand All@@ -429,7 +434,7 @@ def _init_locators(self):
v.append(Size.from_any(self._colorbar_size, sz))
v.append(Size.from_any(self._colorbar_pad, sz))
locator = self._divider.new_locator(nx=0, nx1=-1, ny=0)
for i in range(self.ngrids):
for i in range(self.n_axes):
self.cbar_axes[i].set_visible(False)
self.cbar_axes[0].set_axes_locator(locator)
self.cbar_axes[0].set_visible(True)
Expand DownExpand Up@@ -490,7 +495,7 @@ def _init_locators(self):
v_cb_pos.append(len(v))
v.append(Size.from_any(self._colorbar_size, sz))

for i in range(self.ngrids):
for i in range(self.n_axes):
col, row = self._get_col_row(i)
locator = self._divider.new_locator(nx=h_ax_pos[col],
ny=v_ax_pos[self._nrows-1-row])
Expand DownExpand Up@@ -530,12 +535,12 @@ def _init_locators(self):
v.append(Size.from_any(self._colorbar_size, sz))
locator = self._divider.new_locator(nx=0, nx1=-1, ny=-2)
if cb_location in ("right", "top"):
for i in range(self.ngrids):
for i in range(self.n_axes):
self.cbar_axes[i].set_visible(False)
self.cbar_axes[0].set_axes_locator(locator)
self.cbar_axes[0].set_visible(True)
elif cb_mode == "each":
for i in range(self.ngrids):
for i in range(self.n_axes):
self.cbar_axes[i].set_visible(True)
elif cb_mode == "edge":
if cb_location in ("right", "left"):
Expand All@@ -544,10 +549,10 @@ def _init_locators(self):
count = self._ncols
for i in range(count):
self.cbar_axes[i].set_visible(True)
for j in range(i + 1, self.ngrids):
for j in range(i + 1, self.n_axes):
self.cbar_axes[j].set_visible(False)
else:
for i in range(self.ngrids):
for i in range(self.n_axes):
self.cbar_axes[i].set_visible(False)
self.cbar_axes[i].set_position([1., 1., 0.001, 0.001],
which="active")
Expand Down
17 changes: 11 additions & 6 deletionslib/mpl_toolkits/axes_grid1/tests/test_axes_grid1.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -104,7 +104,6 @@ def test_axesgrid_colorbar_log_smoketest():
fig = plt.figure()
grid = AxesGrid(fig, 111, # modified to be only subplot
nrows_ncols=(1, 1),
ngrids=1,
label_mode="L",
cbar_location="top",
cbar_mode="single",
Expand DownExpand Up@@ -638,15 +637,15 @@ def test_grid_axes_position(direction):
assert loc[3].args[1] == loc[2].args[1]


@pytest.mark.parametrize('rect,ngrids, error, message', (
@pytest.mark.parametrize('rect,n_axes, error, message', (
((1, 1), None, TypeError, "Incorrect rect format"),
(111, -1, ValueError, "ngrids must be positive"),
(111, 7, ValueError, "ngrids must be positive"),
(111, -1, ValueError, "n_axes must be positive"),
(111, 7, ValueError, "n_axes must be positive"),
))
def test_grid_errors(rect,ngrids, error, message):
def test_grid_errors(rect,n_axes, error, message):
fig = plt.figure()
with pytest.raises(error, match=message):
Grid(fig, rect, (2, 3),ngrids=ngrids)
Grid(fig, rect, (2, 3),n_axes=n_axes)


@pytest.mark.parametrize('anchor, error, message', (
Expand DownExpand Up@@ -780,3 +779,9 @@ def test_anchored_locator_base_call():
def test_grid_with_axes_class_not_overriding_axis():
Grid(plt.figure(), 111, (2, 2), axes_class=mpl.axes.Axes)
RGBAxes(plt.figure(), 111, axes_class=mpl.axes.Axes)


def test_grid_n_axes():
fig = plt.figure()
grid = Grid(fig, 111, (3, 3), n_axes=5)
assert len(fig.axes) == grid.n_axes == 5
Loading

[8]ページ先頭

©2009-2025 Movatter.jp