|
3 | 3 | from fgivenx.plot import plot, plot_lines |
4 | 4 | import matplotlib.pyplot as plt |
5 | 5 | import matplotlib |
| 6 | +from matplotlib.testing.decorators import image_comparison |
6 | 7 |
|
7 | 8 |
|
8 | | -def test_plot(): |
| 9 | +def gen_plot_data(): |
| 10 | + numpy.random.seed(0) |
9 | 11 | nx = 100 |
10 | 12 | ny = 101 |
11 | 13 | x = numpy.linspace(0, 1, nx) |
12 | 14 | y = numpy.linspace(0, 1, ny) |
13 | | - z = numpy.random.rand(ny, nx) |
| 15 | + X, Y = numpy.meshgrid(x, y) |
| 16 | + z = numpy.exp(-((X-0.5)**2+(Y-0.5)**2)/0.01) |
| 17 | + return x, y, z |
| 18 | + |
| 19 | + |
| 20 | +@image_comparison(baseline_images=['plot'], extensions=['pdf']) |
| 21 | +def test_plot(): |
| 22 | + x, y, z = gen_plot_data() |
14 | 23 |
|
15 | 24 | fig, ax = plt.subplots() |
16 | 25 | cbar = plot(x, y, z, ax) |
17 | 26 | assert type(cbar) is matplotlib.contour.QuadContourSet |
18 | 27 |
|
| 28 | + |
| 29 | +def test_plot_wrong_argument(): |
| 30 | + x, y, z = gen_plot_data() |
19 | 31 | with pytest.raises(TypeError): |
20 | 32 | plot(x, y, z, wrong_argument=None) |
21 | 33 |
|
22 | | - cbar = plot(x, y, z) |
23 | | - cbar = plot(x, y, z, smooth=1) |
24 | | - cbar = plot(x, y, z, rasterize_contours=True) |
25 | | - cbar = plot(x, y, z, lines=False) |
26 | 34 |
|
| 35 | +@image_comparison(baseline_images=['plot_no_ax'], extensions=['pdf']) |
| 36 | +def test_plot_no_ax(): |
| 37 | + plt.subplots() |
| 38 | + x, y, z = gen_plot_data() |
| 39 | + plot(x, y, z) |
27 | 40 |
|
28 | | -def test_plot_lines(): |
| 41 | + |
| 42 | +@image_comparison(baseline_images=['plot_smooth'], extensions=['pdf']) |
| 43 | +def test_plot_smooth(): |
| 44 | + plt.subplots() |
| 45 | + x, y, z = gen_plot_data() |
| 46 | + plot(x, y, z, smooth=1) |
| 47 | + |
| 48 | + |
| 49 | +@image_comparison(baseline_images=['plot_rasterize'], extensions=['pdf']) |
| 50 | +def test_plot_rasterize(): |
| 51 | + plt.subplots() |
| 52 | + x, y, z = gen_plot_data() |
| 53 | + plot(x, y, z, rasterize_contours=True) |
| 54 | + |
| 55 | + |
| 56 | +@image_comparison(baseline_images=['plot_nolines'], extensions=['pdf']) |
| 57 | +def test_plot_nolines(): |
| 58 | + plt.subplots() |
| 59 | + x, y, z = gen_plot_data() |
| 60 | + plot(x, y, z, lines=False) |
| 61 | + |
| 62 | + |
| 63 | +def gen_line_data(): |
| 64 | + numpy.random.seed(0) |
29 | 65 | nx = 100 |
30 | 66 | nsamps = 150 |
31 | 67 | x = numpy.linspace(0, 1, nx) |
32 | | - m = numpy.random.normal(nsamps) |
33 | | - c = numpy.random.normal(nsamps) |
| 68 | + m = numpy.random.normal(size=nsamps) |
| 69 | + c = numpy.random.normal(size=nsamps) |
34 | 70 | fsamps = numpy.outer(x, m) + c |
| 71 | + return x, fsamps |
| 72 | + |
| 73 | + |
| 74 | +@image_comparison(baseline_images=['plot_lines'], extensions=['pdf']) |
| 75 | +def test_plot_lines(): |
| 76 | + x, fsamps = gen_line_data() |
35 | 77 | fig, ax = plt.subplots() |
36 | 78 | plot_lines(x, fsamps, ax) |
37 | 79 |
|
| 80 | + |
| 81 | +@image_comparison(baseline_images=['plot_lines_no_ax'], extensions=['pdf']) |
| 82 | +def test_plot_lines_no_ax(): |
| 83 | + x, fsamps = gen_line_data() |
| 84 | + plt.subplots() |
38 | 85 | plot_lines(x, fsamps) |
39 | | - plot_lines(x, fsamps, downsample=200) |
| 86 | + |
| 87 | + |
| 88 | +@image_comparison(baseline_images=['plot_lines_downsample'], extensions=['pdf']) |
| 89 | +def test_plot_lines_no_ax(): |
| 90 | + x, fsamps = gen_line_data() |
| 91 | + plt.subplots() |
| 92 | + plot_lines(x, fsamps, downsample=50) |
0 commit comments