test_simplification.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. import base64
  2. import io
  3. import numpy as np
  4. from numpy.testing import assert_array_almost_equal, assert_array_equal
  5. import pytest
  6. from matplotlib.testing.decorators import (
  7. check_figures_equal, image_comparison, remove_ticks_and_titles)
  8. import matplotlib.pyplot as plt
  9. from matplotlib import patches, transforms
  10. from matplotlib.path import Path
  11. # NOTE: All of these tests assume that path.simplify is set to True
  12. # (the default)
  13. @image_comparison(['clipping'], remove_text=True)
  14. def test_clipping():
  15. t = np.arange(0.0, 2.0, 0.01)
  16. s = np.sin(2*np.pi*t)
  17. fig, ax = plt.subplots()
  18. ax.plot(t, s, linewidth=1.0)
  19. ax.set_ylim((-0.20, -0.28))
  20. @image_comparison(['overflow'], remove_text=True)
  21. def test_overflow():
  22. x = np.array([1.0, 2.0, 3.0, 2.0e5])
  23. y = np.arange(len(x))
  24. fig, ax = plt.subplots()
  25. ax.plot(x, y)
  26. ax.set_xlim(2, 6)
  27. @image_comparison(['clipping_diamond'], remove_text=True)
  28. def test_diamond():
  29. x = np.array([0.0, 1.0, 0.0, -1.0, 0.0])
  30. y = np.array([1.0, 0.0, -1.0, 0.0, 1.0])
  31. fig, ax = plt.subplots()
  32. ax.plot(x, y)
  33. ax.set_xlim(-0.6, 0.6)
  34. ax.set_ylim(-0.6, 0.6)
  35. def test_clipping_out_of_bounds():
  36. # Should work on a Path *without* codes.
  37. path = Path([(0, 0), (1, 2), (2, 1)])
  38. simplified = path.cleaned(clip=(10, 10, 20, 20))
  39. assert_array_equal(simplified.vertices, [(0, 0)])
  40. assert simplified.codes == [Path.STOP]
  41. # Should work on a Path *with* codes, and no curves.
  42. path = Path([(0, 0), (1, 2), (2, 1)],
  43. [Path.MOVETO, Path.LINETO, Path.LINETO])
  44. simplified = path.cleaned(clip=(10, 10, 20, 20))
  45. assert_array_equal(simplified.vertices, [(0, 0)])
  46. assert simplified.codes == [Path.STOP]
  47. # A Path with curves does not do any clipping yet.
  48. path = Path([(0, 0), (1, 2), (2, 3)],
  49. [Path.MOVETO, Path.CURVE3, Path.CURVE3])
  50. simplified = path.cleaned()
  51. simplified_clipped = path.cleaned(clip=(10, 10, 20, 20))
  52. assert_array_equal(simplified.vertices, simplified_clipped.vertices)
  53. assert_array_equal(simplified.codes, simplified_clipped.codes)
  54. def test_noise():
  55. np.random.seed(0)
  56. x = np.random.uniform(size=50000) * 50
  57. fig, ax = plt.subplots()
  58. p1 = ax.plot(x, solid_joinstyle='round', linewidth=2.0)
  59. # Ensure that the path's transform takes the new axes limits into account.
  60. fig.canvas.draw()
  61. path = p1[0].get_path()
  62. transform = p1[0].get_transform()
  63. path = transform.transform_path(path)
  64. simplified = path.cleaned(simplify=True)
  65. assert simplified.vertices.size == 25512
  66. def test_antiparallel_simplification():
  67. def _get_simplified(x, y):
  68. fig, ax = plt.subplots()
  69. p1 = ax.plot(x, y)
  70. path = p1[0].get_path()
  71. transform = p1[0].get_transform()
  72. path = transform.transform_path(path)
  73. simplified = path.cleaned(simplify=True)
  74. simplified = transform.inverted().transform_path(simplified)
  75. return simplified
  76. # test ending on a maximum
  77. x = [0, 0, 0, 0, 0, 1]
  78. y = [.5, 1, -1, 1, 2, .5]
  79. simplified = _get_simplified(x, y)
  80. assert_array_almost_equal([[0., 0.5],
  81. [0., -1.],
  82. [0., 2.],
  83. [1., 0.5]],
  84. simplified.vertices[:-2, :])
  85. # test ending on a minimum
  86. x = [0, 0, 0, 0, 0, 1]
  87. y = [.5, 1, -1, 1, -2, .5]
  88. simplified = _get_simplified(x, y)
  89. assert_array_almost_equal([[0., 0.5],
  90. [0., 1.],
  91. [0., -2.],
  92. [1., 0.5]],
  93. simplified.vertices[:-2, :])
  94. # test ending in between
  95. x = [0, 0, 0, 0, 0, 1]
  96. y = [.5, 1, -1, 1, 0, .5]
  97. simplified = _get_simplified(x, y)
  98. assert_array_almost_equal([[0., 0.5],
  99. [0., 1.],
  100. [0., -1.],
  101. [0., 0.],
  102. [1., 0.5]],
  103. simplified.vertices[:-2, :])
  104. # test no anti-parallel ending at max
  105. x = [0, 0, 0, 0, 0, 1]
  106. y = [.5, 1, 2, 1, 3, .5]
  107. simplified = _get_simplified(x, y)
  108. assert_array_almost_equal([[0., 0.5],
  109. [0., 3.],
  110. [1., 0.5]],
  111. simplified.vertices[:-2, :])
  112. # test no anti-parallel ending in middle
  113. x = [0, 0, 0, 0, 0, 1]
  114. y = [.5, 1, 2, 1, 1, .5]
  115. simplified = _get_simplified(x, y)
  116. assert_array_almost_equal([[0., 0.5],
  117. [0., 2.],
  118. [0., 1.],
  119. [1., 0.5]],
  120. simplified.vertices[:-2, :])
  121. # Only consider angles in 0 <= angle <= pi/2, otherwise
  122. # using min/max will get the expected results out of order:
  123. # min/max for simplification code depends on original vector,
  124. # and if angle is outside above range then simplification
  125. # min/max will be opposite from actual min/max.
  126. @pytest.mark.parametrize('angle', [0, np.pi/4, np.pi/3, np.pi/2])
  127. @pytest.mark.parametrize('offset', [0, .5])
  128. def test_angled_antiparallel(angle, offset):
  129. scale = 5
  130. np.random.seed(19680801)
  131. # get 15 random offsets
  132. # TODO: guarantee offset > 0 results in some offsets < 0
  133. vert_offsets = (np.random.rand(15) - offset) * scale
  134. # always start at 0 so rotation makes sense
  135. vert_offsets[0] = 0
  136. # always take the first step the same direction
  137. vert_offsets[1] = 1
  138. # compute points along a diagonal line
  139. x = np.sin(angle) * vert_offsets
  140. y = np.cos(angle) * vert_offsets
  141. # will check these later
  142. x_max = x[1:].max()
  143. x_min = x[1:].min()
  144. y_max = y[1:].max()
  145. y_min = y[1:].min()
  146. if offset > 0:
  147. p_expected = Path([[0, 0],
  148. [x_max, y_max],
  149. [x_min, y_min],
  150. [x[-1], y[-1]],
  151. [0, 0]],
  152. codes=[1, 2, 2, 2, 0])
  153. else:
  154. p_expected = Path([[0, 0],
  155. [x_max, y_max],
  156. [x[-1], y[-1]],
  157. [0, 0]],
  158. codes=[1, 2, 2, 0])
  159. p = Path(np.vstack([x, y]).T)
  160. p2 = p.cleaned(simplify=True)
  161. assert_array_almost_equal(p_expected.vertices,
  162. p2.vertices)
  163. assert_array_equal(p_expected.codes, p2.codes)
  164. def test_sine_plus_noise():
  165. np.random.seed(0)
  166. x = (np.sin(np.linspace(0, np.pi * 2.0, 50000)) +
  167. np.random.uniform(size=50000) * 0.01)
  168. fig, ax = plt.subplots()
  169. p1 = ax.plot(x, solid_joinstyle='round', linewidth=2.0)
  170. # Ensure that the path's transform takes the new axes limits into account.
  171. fig.canvas.draw()
  172. path = p1[0].get_path()
  173. transform = p1[0].get_transform()
  174. path = transform.transform_path(path)
  175. simplified = path.cleaned(simplify=True)
  176. assert simplified.vertices.size == 25240
  177. @image_comparison(['simplify_curve'], remove_text=True, tol=0.017)
  178. def test_simplify_curve():
  179. pp1 = patches.PathPatch(
  180. Path([(0, 0), (1, 0), (1, 1), (np.nan, 1), (0, 0), (2, 0), (2, 2),
  181. (0, 0)],
  182. [Path.MOVETO, Path.CURVE3, Path.CURVE3, Path.CURVE3, Path.CURVE3,
  183. Path.CURVE3, Path.CURVE3, Path.CLOSEPOLY]),
  184. fc="none")
  185. fig, ax = plt.subplots()
  186. ax.add_patch(pp1)
  187. ax.set_xlim((0, 2))
  188. ax.set_ylim((0, 2))
  189. @check_figures_equal()
  190. def test_closed_path_nan_removal(fig_test, fig_ref):
  191. ax_test = fig_test.subplots(2, 2).flatten()
  192. ax_ref = fig_ref.subplots(2, 2).flatten()
  193. # NaN on the first point also removes the last point, because it's closed.
  194. path = Path(
  195. [[-3, np.nan], [3, -3], [3, 3], [-3, 3], [-3, -3]],
  196. [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY])
  197. ax_test[0].add_patch(patches.PathPatch(path, facecolor='none'))
  198. path = Path(
  199. [[-3, np.nan], [3, -3], [3, 3], [-3, 3], [-3, np.nan]],
  200. [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO])
  201. ax_ref[0].add_patch(patches.PathPatch(path, facecolor='none'))
  202. # NaN on second-last point should not re-close.
  203. path = Path(
  204. [[-2, -2], [2, -2], [2, 2], [-2, np.nan], [-2, -2]],
  205. [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY])
  206. ax_test[0].add_patch(patches.PathPatch(path, facecolor='none'))
  207. path = Path(
  208. [[-2, -2], [2, -2], [2, 2], [-2, np.nan], [-2, -2]],
  209. [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO])
  210. ax_ref[0].add_patch(patches.PathPatch(path, facecolor='none'))
  211. # Test multiple loops in a single path (with same paths as above).
  212. path = Path(
  213. [[-3, np.nan], [3, -3], [3, 3], [-3, 3], [-3, -3],
  214. [-2, -2], [2, -2], [2, 2], [-2, np.nan], [-2, -2]],
  215. [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY,
  216. Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY])
  217. ax_test[1].add_patch(patches.PathPatch(path, facecolor='none'))
  218. path = Path(
  219. [[-3, np.nan], [3, -3], [3, 3], [-3, 3], [-3, np.nan],
  220. [-2, -2], [2, -2], [2, 2], [-2, np.nan], [-2, -2]],
  221. [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO,
  222. Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO])
  223. ax_ref[1].add_patch(patches.PathPatch(path, facecolor='none'))
  224. # NaN in first point of CURVE3 should not re-close, and hide entire curve.
  225. path = Path(
  226. [[-1, -1], [1, -1], [1, np.nan], [0, 1], [-1, 1], [-1, -1]],
  227. [Path.MOVETO, Path.LINETO, Path.CURVE3, Path.CURVE3, Path.LINETO,
  228. Path.CLOSEPOLY])
  229. ax_test[2].add_patch(patches.PathPatch(path, facecolor='none'))
  230. path = Path(
  231. [[-1, -1], [1, -1], [1, np.nan], [0, 1], [-1, 1], [-1, -1]],
  232. [Path.MOVETO, Path.LINETO, Path.CURVE3, Path.CURVE3, Path.LINETO,
  233. Path.CLOSEPOLY])
  234. ax_ref[2].add_patch(patches.PathPatch(path, facecolor='none'))
  235. # NaN in second point of CURVE3 should not re-close, and hide entire curve
  236. # plus next line segment.
  237. path = Path(
  238. [[-3, -3], [3, -3], [3, 0], [0, np.nan], [-3, 3], [-3, -3]],
  239. [Path.MOVETO, Path.LINETO, Path.CURVE3, Path.CURVE3, Path.LINETO,
  240. Path.LINETO])
  241. ax_test[2].add_patch(patches.PathPatch(path, facecolor='none'))
  242. path = Path(
  243. [[-3, -3], [3, -3], [3, 0], [0, np.nan], [-3, 3], [-3, -3]],
  244. [Path.MOVETO, Path.LINETO, Path.CURVE3, Path.CURVE3, Path.LINETO,
  245. Path.LINETO])
  246. ax_ref[2].add_patch(patches.PathPatch(path, facecolor='none'))
  247. # NaN in first point of CURVE4 should not re-close, and hide entire curve.
  248. path = Path(
  249. [[-1, -1], [1, -1], [1, np.nan], [0, 0], [0, 1], [-1, 1], [-1, -1]],
  250. [Path.MOVETO, Path.LINETO, Path.CURVE4, Path.CURVE4, Path.CURVE4,
  251. Path.LINETO, Path.CLOSEPOLY])
  252. ax_test[3].add_patch(patches.PathPatch(path, facecolor='none'))
  253. path = Path(
  254. [[-1, -1], [1, -1], [1, np.nan], [0, 0], [0, 1], [-1, 1], [-1, -1]],
  255. [Path.MOVETO, Path.LINETO, Path.CURVE4, Path.CURVE4, Path.CURVE4,
  256. Path.LINETO, Path.CLOSEPOLY])
  257. ax_ref[3].add_patch(patches.PathPatch(path, facecolor='none'))
  258. # NaN in second point of CURVE4 should not re-close, and hide entire curve.
  259. path = Path(
  260. [[-2, -2], [2, -2], [2, 0], [0, np.nan], [0, 2], [-2, 2], [-2, -2]],
  261. [Path.MOVETO, Path.LINETO, Path.CURVE4, Path.CURVE4, Path.CURVE4,
  262. Path.LINETO, Path.LINETO])
  263. ax_test[3].add_patch(patches.PathPatch(path, facecolor='none'))
  264. path = Path(
  265. [[-2, -2], [2, -2], [2, 0], [0, np.nan], [0, 2], [-2, 2], [-2, -2]],
  266. [Path.MOVETO, Path.LINETO, Path.CURVE4, Path.CURVE4, Path.CURVE4,
  267. Path.LINETO, Path.LINETO])
  268. ax_ref[3].add_patch(patches.PathPatch(path, facecolor='none'))
  269. # NaN in third point of CURVE4 should not re-close, and hide entire curve
  270. # plus next line segment.
  271. path = Path(
  272. [[-3, -3], [3, -3], [3, 0], [0, 0], [0, np.nan], [-3, 3], [-3, -3]],
  273. [Path.MOVETO, Path.LINETO, Path.CURVE4, Path.CURVE4, Path.CURVE4,
  274. Path.LINETO, Path.LINETO])
  275. ax_test[3].add_patch(patches.PathPatch(path, facecolor='none'))
  276. path = Path(
  277. [[-3, -3], [3, -3], [3, 0], [0, 0], [0, np.nan], [-3, 3], [-3, -3]],
  278. [Path.MOVETO, Path.LINETO, Path.CURVE4, Path.CURVE4, Path.CURVE4,
  279. Path.LINETO, Path.LINETO])
  280. ax_ref[3].add_patch(patches.PathPatch(path, facecolor='none'))
  281. # Keep everything clean.
  282. for ax in [*ax_test.flat, *ax_ref.flat]:
  283. ax.set(xlim=(-3.5, 3.5), ylim=(-3.5, 3.5))
  284. remove_ticks_and_titles(fig_test)
  285. remove_ticks_and_titles(fig_ref)
  286. @check_figures_equal()
  287. def test_closed_path_clipping(fig_test, fig_ref):
  288. vertices = []
  289. for roll in range(8):
  290. offset = 0.1 * roll + 0.1
  291. # A U-like pattern.
  292. pattern = [
  293. [-0.5, 1.5], [-0.5, -0.5], [1.5, -0.5], [1.5, 1.5], # Outer square
  294. # With a notch in the top.
  295. [1 - offset / 2, 1.5], [1 - offset / 2, offset],
  296. [offset / 2, offset], [offset / 2, 1.5],
  297. ]
  298. # Place the initial/final point anywhere in/out of the clipping area.
  299. pattern = np.roll(pattern, roll, axis=0)
  300. pattern = np.concatenate((pattern, pattern[:1, :]))
  301. vertices.append(pattern)
  302. # Multiple subpaths are used here to ensure they aren't broken by closed
  303. # loop clipping.
  304. codes = np.full(len(vertices[0]), Path.LINETO)
  305. codes[0] = Path.MOVETO
  306. codes[-1] = Path.CLOSEPOLY
  307. codes = np.tile(codes, len(vertices))
  308. vertices = np.concatenate(vertices)
  309. fig_test.set_size_inches((5, 5))
  310. path = Path(vertices, codes)
  311. fig_test.add_artist(patches.PathPatch(path, facecolor='none'))
  312. # For reference, we draw the same thing, but unclosed by using a line to
  313. # the last point only.
  314. fig_ref.set_size_inches((5, 5))
  315. codes = codes.copy()
  316. codes[codes == Path.CLOSEPOLY] = Path.LINETO
  317. path = Path(vertices, codes)
  318. fig_ref.add_artist(patches.PathPatch(path, facecolor='none'))
  319. @image_comparison(['hatch_simplify'], remove_text=True)
  320. def test_hatch():
  321. fig, ax = plt.subplots()
  322. ax.add_patch(plt.Rectangle((0, 0), 1, 1, fill=False, hatch="/"))
  323. ax.set_xlim((0.45, 0.55))
  324. ax.set_ylim((0.45, 0.55))
  325. @image_comparison(['fft_peaks'], remove_text=True)
  326. def test_fft_peaks():
  327. fig, ax = plt.subplots()
  328. t = np.arange(65536)
  329. p1 = ax.plot(abs(np.fft.fft(np.sin(2*np.pi*.01*t)*np.blackman(len(t)))))
  330. # Ensure that the path's transform takes the new axes limits into account.
  331. fig.canvas.draw()
  332. path = p1[0].get_path()
  333. transform = p1[0].get_transform()
  334. path = transform.transform_path(path)
  335. simplified = path.cleaned(simplify=True)
  336. assert simplified.vertices.size == 36
  337. def test_start_with_moveto():
  338. # Should be entirely clipped away to a single MOVETO
  339. data = b"""
  340. ZwAAAAku+v9UAQAA+Tj6/z8CAADpQ/r/KAMAANlO+v8QBAAAyVn6//UEAAC6ZPr/2gUAAKpv+v+8
  341. BgAAm3r6/50HAACLhfr/ewgAAHyQ+v9ZCQAAbZv6/zQKAABepvr/DgsAAE+x+v/lCwAAQLz6/7wM
  342. AAAxx/r/kA0AACPS+v9jDgAAFN36/zQPAAAF6Pr/AxAAAPfy+v/QEAAA6f36/5wRAADbCPv/ZhIA
  343. AMwT+/8uEwAAvh77//UTAACwKfv/uRQAAKM0+/98FQAAlT/7/z0WAACHSvv//RYAAHlV+/+7FwAA
  344. bGD7/3cYAABea/v/MRkAAFF2+//pGQAARIH7/6AaAAA3jPv/VRsAACmX+/8JHAAAHKL7/7ocAAAP
  345. rfv/ah0AAAO4+/8YHgAA9sL7/8QeAADpzfv/bx8AANzY+/8YIAAA0OP7/78gAADD7vv/ZCEAALf5
  346. +/8IIgAAqwT8/6kiAACeD/z/SiMAAJIa/P/oIwAAhiX8/4QkAAB6MPz/HyUAAG47/P+4JQAAYkb8
  347. /1AmAABWUfz/5SYAAEpc/P95JwAAPmf8/wsoAAAzcvz/nCgAACd9/P8qKQAAHIj8/7cpAAAQk/z/
  348. QyoAAAWe/P/MKgAA+aj8/1QrAADus/z/2isAAOO+/P9eLAAA2Mn8/+AsAADM1Pz/YS0AAMHf/P/g
  349. LQAAtur8/10uAACr9fz/2C4AAKEA/f9SLwAAlgv9/8ovAACLFv3/QDAAAIAh/f+1MAAAdSz9/ycx
  350. AABrN/3/mDEAAGBC/f8IMgAAVk39/3UyAABLWP3/4TIAAEFj/f9LMwAANm79/7MzAAAsef3/GjQA
  351. ACKE/f9+NAAAF4/9/+E0AAANmv3/QzUAAAOl/f+iNQAA+a/9/wA2AADvuv3/XDYAAOXF/f+2NgAA
  352. 29D9/w83AADR2/3/ZjcAAMfm/f+7NwAAvfH9/w44AACz/P3/XzgAAKkH/v+vOAAAnxL+//04AACW
  353. Hf7/SjkAAIwo/v+UOQAAgjP+/905AAB5Pv7/JDoAAG9J/v9pOgAAZVT+/606AABcX/7/7zoAAFJq
  354. /v8vOwAASXX+/207AAA/gP7/qjsAADaL/v/lOwAALZb+/x48AAAjof7/VTwAABqs/v+LPAAAELf+
  355. /788AAAHwv7/8TwAAP7M/v8hPQAA9df+/1A9AADr4v7/fT0AAOLt/v+oPQAA2fj+/9E9AADQA///
  356. +T0AAMYO//8fPgAAvRn//0M+AAC0JP//ZT4AAKsv//+GPgAAojr//6U+AACZRf//wj4AAJBQ///d
  357. PgAAh1v///c+AAB+Zv//Dz8AAHRx//8lPwAAa3z//zk/AABih///TD8AAFmS//9dPwAAUJ3//2w/
  358. AABHqP//ej8AAD6z//+FPwAANb7//48/AAAsyf//lz8AACPU//+ePwAAGt///6M/AAAR6v//pj8A
  359. AAj1//+nPwAA/////w=="""
  360. verts = np.frombuffer(base64.decodebytes(data), dtype='<i4')
  361. verts = verts.reshape((len(verts) // 2, 2))
  362. path = Path(verts)
  363. segs = path.iter_segments(transforms.IdentityTransform(),
  364. clip=(0.0, 0.0, 100.0, 100.0))
  365. segs = list(segs)
  366. assert len(segs) == 1
  367. assert segs[0][1] == Path.MOVETO
  368. def test_throw_rendering_complexity_exceeded():
  369. plt.rcParams['path.simplify'] = False
  370. xx = np.arange(2_000_000)
  371. yy = np.random.rand(2_000_000)
  372. yy[1000] = np.nan
  373. fig, ax = plt.subplots()
  374. ax.plot(xx, yy)
  375. with pytest.raises(OverflowError):
  376. fig.savefig(io.BytesIO())
  377. @image_comparison(['clipper_edge'], remove_text=True)
  378. def test_clipper():
  379. dat = (0, 1, 0, 2, 0, 3, 0, 4, 0, 5)
  380. fig = plt.figure(figsize=(2, 1))
  381. fig.subplots_adjust(left=0, bottom=0, wspace=0, hspace=0)
  382. ax = fig.add_axes((0, 0, 1.0, 1.0), ylim=(0, 5), autoscale_on=False)
  383. ax.plot(dat)
  384. ax.xaxis.set_major_locator(plt.MultipleLocator(1))
  385. ax.yaxis.set_major_locator(plt.MultipleLocator(1))
  386. ax.xaxis.set_ticks_position('bottom')
  387. ax.yaxis.set_ticks_position('left')
  388. ax.set_xlim(5, 9)
  389. @image_comparison(['para_equal_perp'], remove_text=True)
  390. def test_para_equal_perp():
  391. x = np.array([0, 1, 2, 1, 0, -1, 0, 1] + [1] * 128)
  392. y = np.array([1, 1, 2, 1, 0, -1, 0, 0] + [0] * 128)
  393. fig, ax = plt.subplots()
  394. ax.plot(x + 1, y + 1)
  395. ax.plot(x + 1, y + 1, 'ro')
  396. @image_comparison(['clipping_with_nans'])
  397. def test_clipping_with_nans():
  398. x = np.linspace(0, 3.14 * 2, 3000)
  399. y = np.sin(x)
  400. x[::100] = np.nan
  401. fig, ax = plt.subplots()
  402. ax.plot(x, y)
  403. ax.set_ylim(-0.25, 0.25)
  404. def test_clipping_full():
  405. p = Path([[1e30, 1e30]] * 5)
  406. simplified = list(p.iter_segments(clip=[0, 0, 100, 100]))
  407. assert simplified == []
  408. p = Path([[50, 40], [75, 65]], [1, 2])
  409. simplified = list(p.iter_segments(clip=[0, 0, 100, 100]))
  410. assert ([(list(x), y) for x, y in simplified] ==
  411. [([50, 40], 1), ([75, 65], 2)])
  412. p = Path([[50, 40]], [1])
  413. simplified = list(p.iter_segments(clip=[0, 0, 100, 100]))
  414. assert ([(list(x), y) for x, y in simplified] ==
  415. [([50, 40], 1)])