test_cycles.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import contextlib
  2. from io import StringIO
  3. import matplotlib as mpl
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import pytest
  7. from cycler import cycler
  8. def test_colorcycle_basic():
  9. fig, ax = plt.subplots()
  10. ax.set_prop_cycle(cycler('color', ['r', 'g', 'y']))
  11. for _ in range(4):
  12. ax.plot(range(10), range(10))
  13. assert [l.get_color() for l in ax.lines] == ['r', 'g', 'y', 'r']
  14. def test_marker_cycle():
  15. fig, ax = plt.subplots()
  16. ax.set_prop_cycle(cycler('c', ['r', 'g', 'y']) +
  17. cycler('marker', ['.', '*', 'x']))
  18. for _ in range(4):
  19. ax.plot(range(10), range(10))
  20. assert [l.get_color() for l in ax.lines] == ['r', 'g', 'y', 'r']
  21. assert [l.get_marker() for l in ax.lines] == ['.', '*', 'x', '.']
  22. def test_marker_cycle_kwargs_arrays_iterators():
  23. fig, ax = plt.subplots()
  24. ax.set_prop_cycle(c=np.array(['r', 'g', 'y']),
  25. marker=iter(['.', '*', 'x']))
  26. for _ in range(4):
  27. ax.plot(range(10), range(10))
  28. assert [l.get_color() for l in ax.lines] == ['r', 'g', 'y', 'r']
  29. assert [l.get_marker() for l in ax.lines] == ['.', '*', 'x', '.']
  30. def test_linestylecycle_basic():
  31. fig, ax = plt.subplots()
  32. ax.set_prop_cycle(cycler('ls', ['-', '--', ':']))
  33. for _ in range(4):
  34. ax.plot(range(10), range(10))
  35. assert [l.get_linestyle() for l in ax.lines] == ['-', '--', ':', '-']
  36. def test_fillcycle_basic():
  37. fig, ax = plt.subplots()
  38. ax.set_prop_cycle(cycler('c', ['r', 'g', 'y']) +
  39. cycler('hatch', ['xx', 'O', '|-']) +
  40. cycler('linestyle', ['-', '--', ':']))
  41. for _ in range(4):
  42. ax.fill(range(10), range(10))
  43. assert ([p.get_facecolor() for p in ax.patches]
  44. == [mpl.colors.to_rgba(c) for c in ['r', 'g', 'y', 'r']])
  45. assert [p.get_hatch() for p in ax.patches] == ['xx', 'O', '|-', 'xx']
  46. assert [p.get_linestyle() for p in ax.patches] == ['-', '--', ':', '-']
  47. def test_fillcycle_ignore():
  48. fig, ax = plt.subplots()
  49. ax.set_prop_cycle(cycler('color', ['r', 'g', 'y']) +
  50. cycler('hatch', ['xx', 'O', '|-']) +
  51. cycler('marker', ['.', '*', 'D']))
  52. t = range(10)
  53. # Should not advance the cycler, even though there is an
  54. # unspecified property in the cycler "marker".
  55. # "marker" is not a Polygon property, and should be ignored.
  56. ax.fill(t, t, 'r', hatch='xx')
  57. # Allow the cycler to advance, but specify some properties
  58. ax.fill(t, t, hatch='O')
  59. ax.fill(t, t)
  60. ax.fill(t, t)
  61. assert ([p.get_facecolor() for p in ax.patches]
  62. == [mpl.colors.to_rgba(c) for c in ['r', 'r', 'g', 'y']])
  63. assert [p.get_hatch() for p in ax.patches] == ['xx', 'O', 'O', '|-']
  64. def test_property_collision_plot():
  65. fig, ax = plt.subplots()
  66. ax.set_prop_cycle('linewidth', [2, 4])
  67. t = range(10)
  68. for c in range(1, 4):
  69. ax.plot(t, t, lw=0.1)
  70. ax.plot(t, t)
  71. ax.plot(t, t)
  72. assert [l.get_linewidth() for l in ax.lines] == [0.1, 0.1, 0.1, 2, 4]
  73. def test_property_collision_fill():
  74. fig, ax = plt.subplots()
  75. ax.set_prop_cycle(linewidth=[2, 3, 4, 5, 6], facecolor='bgcmy')
  76. t = range(10)
  77. for c in range(1, 4):
  78. ax.fill(t, t, lw=0.1)
  79. ax.fill(t, t)
  80. ax.fill(t, t)
  81. assert ([p.get_facecolor() for p in ax.patches]
  82. == [mpl.colors.to_rgba(c) for c in 'bgcmy'])
  83. assert [p.get_linewidth() for p in ax.patches] == [0.1, 0.1, 0.1, 5, 6]
  84. def test_valid_input_forms():
  85. fig, ax = plt.subplots()
  86. # These should not raise an error.
  87. ax.set_prop_cycle(None)
  88. ax.set_prop_cycle(cycler('linewidth', [1, 2]))
  89. ax.set_prop_cycle('color', 'rgywkbcm')
  90. ax.set_prop_cycle('lw', (1, 2))
  91. ax.set_prop_cycle('linewidth', [1, 2])
  92. ax.set_prop_cycle('linewidth', iter([1, 2]))
  93. ax.set_prop_cycle('linewidth', np.array([1, 2]))
  94. ax.set_prop_cycle('color', np.array([[1, 0, 0],
  95. [0, 1, 0],
  96. [0, 0, 1]]))
  97. ax.set_prop_cycle('dashes', [[], [13, 2], [8, 3, 1, 3]])
  98. ax.set_prop_cycle(lw=[1, 2], color=['k', 'w'], ls=['-', '--'])
  99. ax.set_prop_cycle(lw=np.array([1, 2]),
  100. color=np.array(['k', 'w']),
  101. ls=np.array(['-', '--']))
  102. def test_cycle_reset():
  103. fig, ax = plt.subplots()
  104. prop0 = StringIO()
  105. prop1 = StringIO()
  106. prop2 = StringIO()
  107. with contextlib.redirect_stdout(prop0):
  108. plt.getp(ax.plot([1, 2], label="label")[0])
  109. ax.set_prop_cycle(linewidth=[10, 9, 4])
  110. with contextlib.redirect_stdout(prop1):
  111. plt.getp(ax.plot([1, 2], label="label")[0])
  112. assert prop1.getvalue() != prop0.getvalue()
  113. ax.set_prop_cycle(None)
  114. with contextlib.redirect_stdout(prop2):
  115. plt.getp(ax.plot([1, 2], label="label")[0])
  116. assert prop2.getvalue() == prop0.getvalue()
  117. def test_invalid_input_forms():
  118. fig, ax = plt.subplots()
  119. with pytest.raises((TypeError, ValueError)):
  120. ax.set_prop_cycle(1)
  121. with pytest.raises((TypeError, ValueError)):
  122. ax.set_prop_cycle([1, 2])
  123. with pytest.raises((TypeError, ValueError)):
  124. ax.set_prop_cycle('color', 'fish')
  125. with pytest.raises((TypeError, ValueError)):
  126. ax.set_prop_cycle('linewidth', 1)
  127. with pytest.raises((TypeError, ValueError)):
  128. ax.set_prop_cycle('linewidth', {1, 2})
  129. with pytest.raises((TypeError, ValueError)):
  130. ax.set_prop_cycle(linewidth=1, color='r')
  131. with pytest.raises((TypeError, ValueError)):
  132. ax.set_prop_cycle('foobar', [1, 2])
  133. with pytest.raises((TypeError, ValueError)):
  134. ax.set_prop_cycle(foobar=[1, 2])
  135. with pytest.raises((TypeError, ValueError)):
  136. ax.set_prop_cycle(cycler(foobar=[1, 2]))
  137. with pytest.raises(ValueError):
  138. ax.set_prop_cycle(cycler(color='rgb', c='cmy'))