|
2 | 2 | import inspect
|
3 | 3 | from copy import copy
|
4 | 4 | from datetime import datetime
|
| 5 | +from typing import Any, Dict, Union |
5 | 6 |
|
6 | 7 | import numpy as np
|
7 | 8 | import pandas as pd
|
|
27 | 28 | requires_cartopy,
|
28 | 29 | requires_cftime,
|
29 | 30 | requires_matplotlib,
|
| 31 | + requires_matplotlib_3_3_0, |
30 | 32 | requires_nc_time_axis,
|
31 | 33 | requires_seaborn,
|
32 | 34 | )
|
|
35 | 37 | try:
|
36 | 38 | import matplotlib as mpl
|
37 | 39 | import matplotlib.pyplot as plt
|
| 40 | + import mpl_toolkits # type: ignore |
38 | 41 | except ImportError:
|
39 | 42 | pass
|
40 | 43 |
|
@@ -131,8 +134,8 @@ def setup(self):
|
131 | 134 | # Remove all matplotlib figures
|
132 | 135 | plt.close("all")
|
133 | 136 |
|
134 |
| - def pass_in_axis(self, plotmethod): |
135 |
| - fig, axes = plt.subplots(ncols=2) |
| 137 | + def pass_in_axis(self, plotmethod, subplot_kw=None): |
| 138 | + fig, axes = plt.subplots(ncols=2, subplot_kw=subplot_kw) |
136 | 139 | plotmethod(ax=axes[0])
|
137 | 140 | assert axes[0].has_data()
|
138 | 141 |
|
@@ -1106,6 +1109,9 @@ class Common2dMixin:
|
1106 | 1109 | Should have the same name as the method.
|
1107 | 1110 | """
|
1108 | 1111 |
|
| 1112 | + # Needs to be overridden in TestSurface for facet grid plots |
| 1113 | + subplot_kws: Union[Dict[Any, Any], None] = None |
| 1114 | + |
1109 | 1115 | @pytest.fixture(autouse=True)
|
1110 | 1116 | def setUp(self):
|
1111 | 1117 | da = DataArray(
|
@@ -1421,7 +1427,7 @@ def test_colorbar_kwargs(self):
|
1421 | 1427 | def test_verbose_facetgrid(self):
|
1422 | 1428 | a = easy_array((10, 15, 3))
|
1423 | 1429 | d = DataArray(a, dims=["y", "x", "z"])
|
1424 |
| - g = xplt.FacetGrid(d, col="z") |
| 1430 | + g = xplt.FacetGrid(d, col="z", subplot_kws=self.subplot_kws) |
1425 | 1431 | g.map_dataarray(self.plotfunc, "x", "y")
|
1426 | 1432 | for ax in g.axes.flat:
|
1427 | 1433 | assert ax.has_data()
|
@@ -1821,6 +1827,95 @@ def test_origin_overrides_xyincrease(self):
|
1821 | 1827 | assert plt.ylim()[0] < 0
|
1822 | 1828 |
|
1823 | 1829 |
|
| 1830 | +class TestSurface(Common2dMixin, PlotTestCase): |
| 1831 | + |
| 1832 | + plotfunc = staticmethod(xplt.surface) |
| 1833 | + subplot_kws = {"projection": "3d"} |
| 1834 | + |
| 1835 | + def test_primitive_artist_returned(self): |
| 1836 | + artist = self.plotmethod() |
| 1837 | + assert isinstance(artist, mpl_toolkits.mplot3d.art3d.Poly3DCollection) |
| 1838 | + |
| 1839 | + @pytest.mark.slow |
| 1840 | + def test_2d_coord_names(self): |
| 1841 | + self.plotmethod(x="x2d", y="y2d") |
| 1842 | + # make sure labels came out ok |
| 1843 | + ax = plt.gca() |
| 1844 | + assert "x2d" == ax.get_xlabel() |
| 1845 | + assert "y2d" == ax.get_ylabel() |
| 1846 | + assert f"{self.darray.long_name} [{self.darray.units}]" == ax.get_zlabel() |
| 1847 | + |
| 1848 | + def test_xyincrease_false_changes_axes(self): |
| 1849 | + # Does not make sense for surface plots |
| 1850 | + pytest.skip("does not make sense for surface plots") |
| 1851 | + |
| 1852 | + def test_xyincrease_true_changes_axes(self): |
| 1853 | + # Does not make sense for surface plots |
| 1854 | + pytest.skip("does not make sense for surface plots") |
| 1855 | + |
| 1856 | + def test_can_pass_in_axis(self): |
| 1857 | + self.pass_in_axis(self.plotmethod, subplot_kw={"projection": "3d"}) |
| 1858 | + |
| 1859 | + def test_default_cmap(self): |
| 1860 | + # Does not make sense for surface plots with default arguments |
| 1861 | + pytest.skip("does not make sense for surface plots") |
| 1862 | + |
| 1863 | + def test_diverging_color_limits(self): |
| 1864 | + # Does not make sense for surface plots with default arguments |
| 1865 | + pytest.skip("does not make sense for surface plots") |
| 1866 | + |
| 1867 | + def test_colorbar_kwargs(self): |
| 1868 | + # Does not make sense for surface plots with default arguments |
| 1869 | + pytest.skip("does not make sense for surface plots") |
| 1870 | + |
| 1871 | + def test_cmap_and_color_both(self): |
| 1872 | + # Does not make sense for surface plots with default arguments |
| 1873 | + pytest.skip("does not make sense for surface plots") |
| 1874 | + |
| 1875 | + def test_seaborn_palette_as_cmap(self): |
| 1876 | + # seaborn does not work with mpl_toolkits.mplot3d |
| 1877 | + with pytest.raises(ValueError): |
| 1878 | + super().test_seaborn_palette_as_cmap() |
| 1879 | + |
| 1880 | + # Need to modify this test for surface(), because all subplots should have labels, |
| 1881 | + # not just left and bottom |
| 1882 | + @pytest.mark.filterwarnings("ignore:tight_layout cannot") |
| 1883 | + def test_convenient_facetgrid(self): |
| 1884 | + a = easy_array((10, 15, 4)) |
| 1885 | + d = DataArray(a, dims=["y", "x", "z"]) |
| 1886 | + g = self.plotfunc(d, x="x", y="y", col="z", col_wrap=2) |
| 1887 | + |
| 1888 | + assert_array_equal(g.axes.shape, [2, 2]) |
| 1889 | + for (y, x), ax in np.ndenumerate(g.axes): |
| 1890 | + assert ax.has_data() |
| 1891 | + assert "y" == ax.get_ylabel() |
| 1892 | + assert "x" == ax.get_xlabel() |
| 1893 | + |
| 1894 | + # Infering labels |
| 1895 | + g = self.plotfunc(d, col="z", col_wrap=2) |
| 1896 | + assert_array_equal(g.axes.shape, [2, 2]) |
| 1897 | + for (y, x), ax in np.ndenumerate(g.axes): |
| 1898 | + assert ax.has_data() |
| 1899 | + assert "y" == ax.get_ylabel() |
| 1900 | + assert "x" == ax.get_xlabel() |
| 1901 | + |
| 1902 | + @requires_matplotlib_3_3_0 |
| 1903 | + def test_viridis_cmap(self): |
| 1904 | + return super().test_viridis_cmap() |
| 1905 | + |
| 1906 | + @requires_matplotlib_3_3_0 |
| 1907 | + def test_can_change_default_cmap(self): |
| 1908 | + return super().test_can_change_default_cmap() |
| 1909 | + |
| 1910 | + @requires_matplotlib_3_3_0 |
| 1911 | + def test_colorbar_default_label(self): |
| 1912 | + return super().test_colorbar_default_label() |
| 1913 | + |
| 1914 | + @requires_matplotlib_3_3_0 |
| 1915 | + def test_facetgrid_map_only_appends_mappables(self): |
| 1916 | + return super().test_facetgrid_map_only_appends_mappables() |
| 1917 | + |
| 1918 | + |
1824 | 1919 | class TestFacetGrid(PlotTestCase):
|
1825 | 1920 | @pytest.fixture(autouse=True)
|
1826 | 1921 | def setUp(self):
|
|
0 commit comments