test_patheffects.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import numpy as np
  2. from matplotlib.testing.decorators import image_comparison
  3. import matplotlib.pyplot as plt
  4. import matplotlib.patheffects as path_effects
  5. from matplotlib.path import Path
  6. import matplotlib.patches as patches
  7. @image_comparison(['patheffect1'], remove_text=True)
  8. def test_patheffect1():
  9. ax1 = plt.subplot()
  10. ax1.imshow([[1, 2], [2, 3]])
  11. txt = ax1.annotate("test", (1., 1.), (0., 0),
  12. arrowprops=dict(arrowstyle="->",
  13. connectionstyle="angle3", lw=2),
  14. size=20, ha="center",
  15. path_effects=[path_effects.withStroke(linewidth=3,
  16. foreground="w")])
  17. txt.arrow_patch.set_path_effects([path_effects.Stroke(linewidth=5,
  18. foreground="w"),
  19. path_effects.Normal()])
  20. pe = [path_effects.withStroke(linewidth=3, foreground="w")]
  21. ax1.grid(True, linestyle="-", path_effects=pe)
  22. @image_comparison(['patheffect2'], remove_text=True, style='mpl20')
  23. def test_patheffect2():
  24. ax2 = plt.subplot()
  25. arr = np.arange(25).reshape((5, 5))
  26. ax2.imshow(arr, interpolation='nearest')
  27. cntr = ax2.contour(arr, colors="k")
  28. cntr.set(path_effects=[path_effects.withStroke(linewidth=3, foreground="w")])
  29. clbls = ax2.clabel(cntr, fmt="%2.0f", use_clabeltext=True)
  30. plt.setp(clbls,
  31. path_effects=[path_effects.withStroke(linewidth=3,
  32. foreground="w")])
  33. @image_comparison(['patheffect3'])
  34. def test_patheffect3():
  35. p1, = plt.plot([1, 3, 5, 4, 3], 'o-b', lw=4)
  36. p1.set_path_effects([path_effects.SimpleLineShadow(),
  37. path_effects.Normal()])
  38. plt.title(
  39. r'testing$^{123}$',
  40. path_effects=[path_effects.withStroke(linewidth=1, foreground="r")])
  41. leg = plt.legend([p1], [r'Line 1$^2$'], fancybox=True, loc='upper left')
  42. leg.legendPatch.set_path_effects([path_effects.withSimplePatchShadow()])
  43. text = plt.text(2, 3, 'Drop test', color='white',
  44. bbox={'boxstyle': 'circle,pad=0.1', 'color': 'red'})
  45. pe = [path_effects.Stroke(linewidth=3.75, foreground='k'),
  46. path_effects.withSimplePatchShadow((6, -3), shadow_rgbFace='blue')]
  47. text.set_path_effects(pe)
  48. text.get_bbox_patch().set_path_effects(pe)
  49. pe = [path_effects.PathPatchEffect(offset=(4, -4), hatch='xxxx',
  50. facecolor='gray'),
  51. path_effects.PathPatchEffect(edgecolor='white', facecolor='black',
  52. lw=1.1)]
  53. t = plt.gcf().text(0.02, 0.1, 'Hatch shadow', fontsize=75, weight=1000,
  54. va='center')
  55. t.set_path_effects(pe)
  56. @image_comparison(['stroked_text.png'])
  57. def test_patheffects_stroked_text():
  58. text_chunks = [
  59. 'A B C D E F G H I J K L',
  60. 'M N O P Q R S T U V W',
  61. 'X Y Z a b c d e f g h i j',
  62. 'k l m n o p q r s t u v',
  63. 'w x y z 0123456789',
  64. r"!@#$%^&*()-=_+[]\;'",
  65. ',./{}|:"<>?'
  66. ]
  67. font_size = 50
  68. ax = plt.axes((0, 0, 1, 1))
  69. for i, chunk in enumerate(text_chunks):
  70. text = ax.text(x=0.01, y=(0.9 - i * 0.13), s=chunk,
  71. fontdict={'ha': 'left', 'va': 'center',
  72. 'size': font_size, 'color': 'white'})
  73. text.set_path_effects([path_effects.Stroke(linewidth=font_size / 10,
  74. foreground='black'),
  75. path_effects.Normal()])
  76. ax.set_xlim(0, 1)
  77. ax.set_ylim(0, 1)
  78. ax.axis('off')
  79. def test_PathEffect_points_to_pixels():
  80. fig = plt.figure(dpi=150)
  81. p1, = plt.plot(range(10))
  82. p1.set_path_effects([path_effects.SimpleLineShadow(),
  83. path_effects.Normal()])
  84. renderer = fig.canvas.get_renderer()
  85. pe_renderer = path_effects.PathEffectRenderer(
  86. p1.get_path_effects(), renderer)
  87. # Confirm that using a path effects renderer maintains point sizes
  88. # appropriately. Otherwise rendered font would be the wrong size.
  89. assert renderer.points_to_pixels(15) == pe_renderer.points_to_pixels(15)
  90. def test_SimplePatchShadow_offset():
  91. pe = path_effects.SimplePatchShadow(offset=(4, 5))
  92. assert pe._offset == (4, 5)
  93. @image_comparison(['collection'], tol=0.03, style='mpl20')
  94. def test_collection():
  95. x, y = np.meshgrid(np.linspace(0, 10, 150), np.linspace(-5, 5, 100))
  96. data = np.sin(x) + np.cos(y)
  97. cs = plt.contour(data)
  98. cs.set(path_effects=[
  99. path_effects.PathPatchEffect(edgecolor='black', facecolor='none', linewidth=12),
  100. path_effects.Stroke(linewidth=5)])
  101. for text in plt.clabel(cs, colors='white'):
  102. text.set_path_effects([path_effects.withStroke(foreground='k',
  103. linewidth=3)])
  104. text.set_bbox({'boxstyle': 'sawtooth', 'facecolor': 'none',
  105. 'edgecolor': 'blue'})
  106. @image_comparison(['tickedstroke'], remove_text=True, extensions=['png'],
  107. tol=0.22) # Increased tolerance due to fixed clipping.
  108. def test_tickedstroke():
  109. fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(12, 4))
  110. path = Path.unit_circle()
  111. patch = patches.PathPatch(path, facecolor='none', lw=2, path_effects=[
  112. path_effects.withTickedStroke(angle=-90, spacing=10,
  113. length=1)])
  114. ax1.add_patch(patch)
  115. ax1.axis('equal')
  116. ax1.set_xlim(-2, 2)
  117. ax1.set_ylim(-2, 2)
  118. ax2.plot([0, 1], [0, 1], label=' ',
  119. path_effects=[path_effects.withTickedStroke(spacing=7,
  120. angle=135)])
  121. nx = 101
  122. x = np.linspace(0.0, 1.0, nx)
  123. y = 0.3 * np.sin(x * 8) + 0.4
  124. ax2.plot(x, y, label=' ', path_effects=[path_effects.withTickedStroke()])
  125. ax2.legend()
  126. nx = 101
  127. ny = 105
  128. # Set up survey vectors
  129. xvec = np.linspace(0.001, 4.0, nx)
  130. yvec = np.linspace(0.001, 4.0, ny)
  131. # Set up survey matrices. Design disk loading and gear ratio.
  132. x1, x2 = np.meshgrid(xvec, yvec)
  133. # Evaluate some stuff to plot
  134. g1 = -(3 * x1 + x2 - 5.5)
  135. g2 = -(x1 + 2 * x2 - 4)
  136. g3 = .8 + x1 ** -3 - x2
  137. cg1 = ax3.contour(x1, x2, g1, [0], colors=('k',))
  138. cg1.set(path_effects=[path_effects.withTickedStroke(angle=135)])
  139. cg2 = ax3.contour(x1, x2, g2, [0], colors=('r',))
  140. cg2.set(path_effects=[path_effects.withTickedStroke(angle=60, length=2)])
  141. cg3 = ax3.contour(x1, x2, g3, [0], colors=('b',))
  142. cg3.set(path_effects=[path_effects.withTickedStroke(spacing=7)])
  143. ax3.set_xlim(0, 4)
  144. ax3.set_ylim(0, 4)
  145. @image_comparison(['spaces_and_newlines.png'], remove_text=True)
  146. def test_patheffects_spaces_and_newlines():
  147. ax = plt.subplot()
  148. s1 = " "
  149. s2 = "\nNewline also causes problems"
  150. text1 = ax.text(0.5, 0.75, s1, ha='center', va='center', size=20,
  151. bbox={'color': 'salmon'})
  152. text2 = ax.text(0.5, 0.25, s2, ha='center', va='center', size=20,
  153. bbox={'color': 'thistle'})
  154. text1.set_path_effects([path_effects.Normal()])
  155. text2.set_path_effects([path_effects.Normal()])