|
3 | 3 | import numpy as np
|
4 | 4 | import pytest
|
5 | 5 |
|
| 6 | +import matplotlib as mpl |
6 | 7 | from matplotlib.axes import Axes, SubplotBase
|
7 | 8 | import matplotlib.pyplot as plt
|
8 | 9 | from matplotlib.testing.decorators import check_figures_equal, image_comparison
|
@@ -260,3 +261,68 @@ def test_old_subplot_compat():
|
260 | 261 | assert not isinstance(fig.add_axes(rect=[0, 0, 1, 1]), SubplotBase)
|
261 | 262 | with pytest.raises(TypeError):
|
262 | 263 | Axes(fig, [0, 0, 1, 1], rect=[0, 0, 1, 1])
|
| 264 | + |
| 265 | + |
| 266 | +class TestAxesArray: |
| 267 | + @staticmethod |
| 268 | + def contain_same_axes(axs1, axs2): |
| 269 | + return all(ax1 is ax2 for ax1, ax2 in zip(axs1.flat, axs2.flat)) |
| 270 | + |
| 271 | + def test_1d(self): |
| 272 | + axs = plt.figure().subplots(1, 3) |
| 273 | + # shape and size |
| 274 | + assert axs.shape == (3,) |
| 275 | + assert axs.size == 3 |
| 276 | + assert axs.ndim == 1 |
| 277 | + # flat |
| 278 | + assert all(isinstance(ax, Axes) for ax in axs.flat) |
| 279 | + assert len(set(id(ax) for ax in axs.flat)) == 3 |
| 280 | + # single index |
| 281 | + assert all(isinstance(axs[i], Axes) for i in range(axs.size)) |
| 282 | + assert len(set(axs[i] for i in range(axs.size))) == 3 |
| 283 | +# iteration |
| 284 | + assert all(ax1 is ax2 for ax1, ax2 in zip(axs, axs.flat)) |
| 285 | + |
| 286 | + def test_1d_no_squeeze(self): |
| 287 | + axs = plt.figure().subplots(1, 3, squeeze=False) |
| 288 | + # shape and size |
| 289 | + assert axs.shape == (1, 3) |
| 290 | + assert axs.size == 3 |
| 291 | + assert axs.ndim == 2 |
| 292 | + # flat |
| 293 | + assert all(isinstance(ax, Axes) for ax in axs.flat) |
| 294 | + assert len(set(id(ax) for ax in axs.flat)) == 3 |
| 295 | + # 2d indexing |
| 296 | + assert axs[0, 0] is axs.flat[0] |
| 297 | + assert axs[0, 2] is axs.flat[-1] |
| 298 | + # single index |
| 299 | + axs_type = type(axs) |
| 300 | + assert type(axs[0]) is axs_type |
| 301 | + assert axs[0].shape == (3,) |
| 302 | + # iteration |
| 303 | + assert all(self.contain_same_axes(axi, axs[i]) for i, axi in enumerate(axs)) |
| 304 | + |
| 305 | + def test_2d(self): |
| 306 | + axs = plt.figure().subplots(2, 3) |
| 307 | + # shape and size |
| 308 | + assert axs.shape == (2, 3) |
| 309 | + assert axs.size == 6 |
| 310 | + assert axs.ndim == 2 |
| 311 | + # flat |
| 312 | + assert all(isinstance(ax, Axes) for ax in axs.flat) |
| 313 | + assert len(set(id(ax) for ax in axs.flat)) == 6 |
| 314 | + # 2d indexing |
| 315 | + assert axs[0, 0] is axs.flat[0] |
| 316 | + assert axs[1, 2] is axs.flat[-1] |
| 317 | + # single index |
| 318 | + axs_type = type(axs) |
| 319 | + assert type(axs[0]) is axs_type |
| 320 | + assert axs[0].shape == (3,) |
| 321 | + # iteration |
| 322 | + assert all(self.contain_same_axes(axi, axs[i]) for i, axi in enumerate(axs)) |
| 323 | + |
| 324 | + def test_deprecated(self): |
| 325 | + axs = plt.figure().subplots(2, 3) |
| 326 | + with pytest.warns(mpl.MatplotlibDeprecationWarning, |
| 327 | + match="Using 'flatten' on AxesArray"): |
| 328 | + axs.flatten() |
0 commit comments