test_common.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import pytest
  2. import pandas.util._test_decorators as td
  3. from pandas import DataFrame
  4. from pandas.tests.plotting.common import (
  5. TestPlotBase,
  6. _check_plot_works,
  7. _gen_two_subplots,
  8. )
  9. @td.skip_if_no_mpl
  10. class TestCommon(TestPlotBase):
  11. def test__check_ticks_props(self):
  12. # GH 34768
  13. df = DataFrame({"b": [0, 1, 0], "a": [1, 2, 3]})
  14. ax = _check_plot_works(df.plot, rot=30)
  15. ax.yaxis.set_tick_params(rotation=30)
  16. msg = "expected 0.00000 but got "
  17. with pytest.raises(AssertionError, match=msg):
  18. self._check_ticks_props(ax, xrot=0)
  19. with pytest.raises(AssertionError, match=msg):
  20. self._check_ticks_props(ax, xlabelsize=0)
  21. with pytest.raises(AssertionError, match=msg):
  22. self._check_ticks_props(ax, yrot=0)
  23. with pytest.raises(AssertionError, match=msg):
  24. self._check_ticks_props(ax, ylabelsize=0)
  25. def test__gen_two_subplots_with_ax(self):
  26. fig = self.plt.gcf()
  27. gen = _gen_two_subplots(f=lambda **kwargs: None, fig=fig, ax="test")
  28. # On the first yield, no subplot should be added since ax was passed
  29. next(gen)
  30. assert fig.get_axes() == []
  31. # On the second, the one axis should match fig.subplot(2, 1, 2)
  32. next(gen)
  33. axes = fig.get_axes()
  34. assert len(axes) == 1
  35. subplot_geometry = list(axes[0].get_subplotspec().get_geometry()[:-1])
  36. subplot_geometry[-1] += 1
  37. assert subplot_geometry == [2, 1, 2]
  38. def test_colorbar_layout(self):
  39. fig = self.plt.figure()
  40. axes = fig.subplot_mosaic(
  41. """
  42. AB
  43. CC
  44. """
  45. )
  46. x = [1, 2, 3]
  47. y = [1, 2, 3]
  48. cs0 = axes["A"].scatter(x, y)
  49. axes["B"].scatter(x, y)
  50. fig.colorbar(cs0, ax=[axes["A"], axes["B"]], location="right")
  51. DataFrame(x).plot(ax=axes["C"])