|
| 1 | +import numpy as np |
| 2 | +import matplotlib |
| 3 | +from matplotlib.testing.decorators import image_comparison, knownfailureif |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +from matplotlib import patches, path |
| 6 | + |
| 7 | +from pylab import * |
| 8 | +import numpy as np |
| 9 | +from matplotlib import patches, path |
| 10 | +nan = np.nan |
| 11 | +Path = path.Path |
| 12 | + |
| 13 | +# NOTE: All of these tests assume that path.simplify is set to True |
| 14 | +# (the default) |
| 15 | + |
| 16 | +@image_comparison(baseline_images=['clipping']) |
| 17 | +def test_clipping(): |
| 18 | + t = np.arange(0.0, 2.0, 0.01) |
| 19 | + s = np.sin(2*pi*t) |
| 20 | + |
| 21 | + fig = plt.figure() |
| 22 | + ax = fig.add_subplot(111) |
| 23 | + ax.plot(t, s, linewidth=1.0) |
| 24 | + ax.set_ylim((-0.20, -0.28)) |
| 25 | + ax.set_xticks([]) |
| 26 | + ax.set_yticks([]) |
| 27 | + fig.savefig('clipping') |
| 28 | + |
| 29 | +@image_comparison(baseline_images=['overflow']) |
| 30 | +def test_overflow(): |
| 31 | + x = np.array([1.0,2.0,3.0,2.0e5]) |
| 32 | + y = np.arange(len(x)) |
| 33 | + |
| 34 | + fig = plt.figure() |
| 35 | + ax = fig.add_subplot(111) |
| 36 | + ax.plot(x,y) |
| 37 | + ax.set_xlim(xmin=2,xmax=6) |
| 38 | + ax.set_xticks([]) |
| 39 | + ax.set_yticks([]) |
| 40 | + |
| 41 | + fig.savefig('overflow') |
| 42 | + |
| 43 | +@image_comparison(baseline_images=['clipping_diamond']) |
| 44 | +def test_diamond(): |
| 45 | + x = np.array([0.0, 1.0, 0.0, -1.0, 0.0]) |
| 46 | + y = np.array([1.0, 0.0, -1.0, 0.0, 1.0]) |
| 47 | + |
| 48 | + fig = plt.figure() |
| 49 | + ax = fig.add_subplot(111) |
| 50 | + ax.plot(x, y) |
| 51 | + ax.set_xlim(xmin=-0.6, xmax=0.6) |
| 52 | + ax.set_ylim(ymin=-0.6, ymax=0.6) |
| 53 | + ax.set_xticks([]) |
| 54 | + ax.set_yticks([]) |
| 55 | + |
| 56 | + fig.savefig('clipping_diamond') |
| 57 | + |
| 58 | +def test_noise(): |
| 59 | + np.random.seed(0) |
| 60 | + x = np.random.uniform(size=(5000,)) * 50 |
| 61 | + |
| 62 | + fig = plt.figure() |
| 63 | + ax = fig.add_subplot(111) |
| 64 | + p1 = ax.plot(x, solid_joinstyle='round', linewidth=2.0) |
| 65 | + ax.set_xticks([]) |
| 66 | + ax.set_yticks([]) |
| 67 | + |
| 68 | + path = p1[0].get_path() |
| 69 | + transform = p1[0].get_transform() |
| 70 | + path = transform.transform_path(path) |
| 71 | + simplified = list(path.iter_segments(simplify=(800, 600))) |
| 72 | + |
| 73 | + assert len(simplified) == 2662 |
| 74 | + |
| 75 | +def test_sine_plus_noise(): |
| 76 | + np.random.seed(0) |
| 77 | + x = np.sin(np.linspace(0, np.pi * 2.0, 1000)) + np.random.uniform(size=(1000,)) * 0.01 |
| 78 | + |
| 79 | + fig = plt.figure() |
| 80 | + ax = fig.add_subplot(111) |
| 81 | + p1 = ax.plot(x, solid_joinstyle='round', linewidth=2.0) |
| 82 | + ax.set_xticks([]) |
| 83 | + ax.set_yticks([]) |
| 84 | + |
| 85 | + path = p1[0].get_path() |
| 86 | + transform = p1[0].get_transform() |
| 87 | + path = transform.transform_path(path) |
| 88 | + simplified = list(path.iter_segments(simplify=(800, 600))) |
| 89 | + |
| 90 | + assert len(simplified) == 279 |
| 91 | + |
| 92 | +@image_comparison(baseline_images=['simplify_curve']) |
| 93 | +def test_simplify_curve(): |
| 94 | + pp1 = patches.PathPatch( |
| 95 | + Path([(0, 0), (1, 0), (1, 1), (nan, 1), (0, 0), (2, 0), (2, 2), (0, 0)], |
| 96 | + [Path.MOVETO, Path.CURVE3, Path.CURVE3, Path.CURVE3, Path.CURVE3, Path.CURVE3, Path.CURVE3, Path.CLOSEPOLY]), |
| 97 | + fc="none") |
| 98 | + |
| 99 | + fig = plt.figure() |
| 100 | + ax = fig.add_subplot(111) |
| 101 | + ax.add_patch(pp1) |
| 102 | + ax.set_xticks([]) |
| 103 | + ax.set_yticks([]) |
| 104 | + ax.set_xlim((0, 2)) |
| 105 | + ax.set_ylim((0, 2)) |
| 106 | + |
| 107 | + fig.savefig('simplify_curve') |
| 108 | + |
| 109 | +if __name__=='__main__': |
| 110 | + import nose |
| 111 | + nose.runmodule(argv=['-s','--with-doctest'], exit=False) |
0 commit comments