test_patheffects.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import numpy as np
  2. import pytest
  3. from matplotlib.testing.decorators import image_comparison
  4. import matplotlib.pyplot as plt
  5. import matplotlib.patheffects as path_effects
  6. @image_comparison(['patheffect1'], remove_text=True)
  7. def test_patheffect1():
  8. ax1 = plt.subplot(111)
  9. ax1.imshow([[1, 2], [2, 3]])
  10. txt = ax1.annotate("test", (1., 1.), (0., 0),
  11. arrowprops=dict(arrowstyle="->",
  12. connectionstyle="angle3", lw=2),
  13. size=20, ha="center",
  14. path_effects=[path_effects.withStroke(linewidth=3,
  15. foreground="w")])
  16. txt.arrow_patch.set_path_effects([path_effects.Stroke(linewidth=5,
  17. foreground="w"),
  18. path_effects.Normal()])
  19. pe = [path_effects.withStroke(linewidth=3, foreground="w")]
  20. ax1.grid(True, linestyle="-", path_effects=pe)
  21. @image_comparison(['patheffect2'], remove_text=True, style='mpl20')
  22. def test_patheffect2():
  23. ax2 = plt.subplot(111)
  24. arr = np.arange(25).reshape((5, 5))
  25. ax2.imshow(arr, interpolation='nearest')
  26. cntr = ax2.contour(arr, colors="k")
  27. plt.setp(cntr.collections,
  28. path_effects=[path_effects.withStroke(linewidth=3,
  29. foreground="w")])
  30. clbls = ax2.clabel(cntr, fmt="%2.0f", use_clabeltext=True)
  31. plt.setp(clbls,
  32. path_effects=[path_effects.withStroke(linewidth=3,
  33. foreground="w")])
  34. @image_comparison(['patheffect3'])
  35. def test_patheffect3():
  36. p1, = plt.plot([1, 3, 5, 4, 3], 'o-b', lw=4)
  37. p1.set_path_effects([path_effects.SimpleLineShadow(),
  38. path_effects.Normal()])
  39. plt.title(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.02, 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. pe = [path_effects.PathPatchEffect(edgecolor='black', facecolor='none',
  99. linewidth=12),
  100. path_effects.Stroke(linewidth=5)]
  101. for collection in cs.collections:
  102. collection.set_path_effects(pe)
  103. for text in plt.clabel(cs, colors='white'):
  104. text.set_path_effects([path_effects.withStroke(foreground='k',
  105. linewidth=3)])
  106. text.set_bbox({'boxstyle': 'sawtooth', 'facecolor': 'none',
  107. 'edgecolor': 'blue'})