|
9 | 9 | import xarray as xr |
10 | 10 |
|
11 | 11 | import xarray_plotly # noqa: F401 - registers accessor |
12 | | -from xarray_plotly import xpx |
| 12 | +from xarray_plotly import plotting, xpx |
| 13 | +from xarray_plotly.plotting import _imshow_supports_facet_row |
13 | 14 |
|
14 | 15 |
|
15 | 16 | class TestXpxFunction: |
@@ -403,6 +404,100 @@ def test_imshow_animation_consistent_bounds(self) -> None: |
403 | 404 | assert coloraxis.cmax == 70.0 |
404 | 405 |
|
405 | 406 |
|
| 407 | +requires_imshow_facet_row = pytest.mark.skipif( |
| 408 | + not _imshow_supports_facet_row(), |
| 409 | + reason="facet_row in px.imshow requires plotly>=6.7.0", |
| 410 | +) |
| 411 | + |
| 412 | + |
| 413 | +class TestImshowFaceting: |
| 414 | + """Tests for imshow facet_col and facet_row.""" |
| 415 | + |
| 416 | + @pytest.fixture(autouse=True) |
| 417 | + def setup(self) -> None: |
| 418 | + """Set up test data.""" |
| 419 | + self.da_3d = xr.DataArray( |
| 420 | + np.random.rand(4, 5, 3), |
| 421 | + dims=["lat", "lon", "scenario"], |
| 422 | + coords={"scenario": ["a", "b", "c"]}, |
| 423 | + name="temperature", |
| 424 | + ) |
| 425 | + self.da_4d = xr.DataArray( |
| 426 | + np.random.rand(4, 5, 2, 3), |
| 427 | + dims=["lat", "lon", "scenario", "year"], |
| 428 | + coords={"scenario": ["low", "high"], "year": [2020, 2021, 2022]}, |
| 429 | + name="temperature", |
| 430 | + ) |
| 431 | + |
| 432 | + def test_imshow_facet_col(self) -> None: |
| 433 | + """Test imshow with facet_col creates one subplot per value.""" |
| 434 | + fig = self.da_3d.plotly.imshow() |
| 435 | + assert len(fig.data) == 3 |
| 436 | + xaxes = [k for k in fig.layout if k.startswith("xaxis")] |
| 437 | + assert len(xaxes) == 3 |
| 438 | + |
| 439 | + @requires_imshow_facet_row |
| 440 | + def test_imshow_facet_row_explicit(self) -> None: |
| 441 | + """Test imshow with explicit facet_row creates one subplot row per value.""" |
| 442 | + fig = self.da_3d.plotly.imshow(facet_col=None, facet_row="scenario") |
| 443 | + assert len(fig.data) == 3 |
| 444 | + yaxes = [k for k in fig.layout if k.startswith("yaxis")] |
| 445 | + assert len(yaxes) == 3 |
| 446 | + annotations = {a.text for a in fig.layout.annotations} |
| 447 | + assert annotations == {"scenario=a", "scenario=b", "scenario=c"} |
| 448 | + |
| 449 | + @requires_imshow_facet_row |
| 450 | + def test_imshow_facet_row_auto_4d(self) -> None: |
| 451 | + """Test that a 4D array auto-assigns facet_col and facet_row.""" |
| 452 | + fig = self.da_4d.plotly.imshow() |
| 453 | + # 2 facet columns (scenario) x 3 facet rows (year) |
| 454 | + assert len(fig.data) == 6 |
| 455 | + annotations = {a.text for a in fig.layout.annotations} |
| 456 | + assert annotations == { |
| 457 | + "scenario=low", |
| 458 | + "scenario=high", |
| 459 | + "year=2020", |
| 460 | + "year=2021", |
| 461 | + "year=2022", |
| 462 | + } |
| 463 | + |
| 464 | + @requires_imshow_facet_row |
| 465 | + def test_imshow_facet_grid_consistent_bounds(self) -> None: |
| 466 | + """Test that facet grid subplots share global color bounds.""" |
| 467 | + da = xr.DataArray( |
| 468 | + np.arange(24, dtype=float).reshape(2, 2, 2, 3), |
| 469 | + dims=["y", "x", "scenario", "year"], |
| 470 | + ) |
| 471 | + fig = da.plotly.imshow() |
| 472 | + coloraxis = fig.layout.coloraxis |
| 473 | + assert coloraxis.cmin == 0.0 |
| 474 | + assert coloraxis.cmax == 23.0 |
| 475 | + |
| 476 | + @requires_imshow_facet_row |
| 477 | + def test_imshow_facet_grid_with_animation(self) -> None: |
| 478 | + """Test imshow with facet_col, facet_row, and animation_frame together.""" |
| 479 | + da = xr.DataArray( |
| 480 | + np.random.rand(4, 5, 2, 3, 6), |
| 481 | + dims=["lat", "lon", "scenario", "year", "time"], |
| 482 | + name="temperature", |
| 483 | + ) |
| 484 | + fig = da.plotly.imshow() |
| 485 | + assert len(fig.data) == 6 |
| 486 | + assert len(fig.frames) == 6 |
| 487 | + |
| 488 | + def test_imshow_facet_row_unsupported_error(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 489 | + """Test informative error when plotly is too old for facet_row.""" |
| 490 | + monkeypatch.setattr(plotting, "_imshow_supports_facet_row", lambda: False) |
| 491 | + with pytest.raises(ValueError, match=r"facet_row for imshow requires plotly>=6\.7\.0"): |
| 492 | + self.da_3d.plotly.imshow(facet_col=None, facet_row="scenario") |
| 493 | + |
| 494 | + def test_imshow_facet_row_none_on_old_plotly(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 495 | + """Test that imshow still works on old plotly when facet_row is not used.""" |
| 496 | + monkeypatch.setattr(plotting, "_imshow_supports_facet_row", lambda: False) |
| 497 | + fig = self.da_3d.plotly.imshow(facet_row=None) |
| 498 | + assert isinstance(fig, go.Figure) |
| 499 | + |
| 500 | + |
406 | 501 | class TestColorsParameter: |
407 | 502 | """Tests for the unified colors parameter.""" |
408 | 503 |
|
|
0 commit comments