test_common.py 1.8 KB

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