test_cycles.py 5.5 KB

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