|
| 1 | +import pytest |
| 2 | + |
| 3 | +import proplot as plt, numpy as np |
| 4 | +from matplotlib.testing import setup |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture() |
| 8 | +def setup_mpl(): |
| 9 | + setup() |
| 10 | + plt.clf() |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.mpl_image_compare |
| 14 | +def test_standardized_input(): |
| 15 | + # Sample data |
| 16 | + state = np.random.RandomState(51423) |
| 17 | + x = y = np.array([-10, -5, 0, 5, 10]) |
| 18 | + xedges = plt.edges(x) |
| 19 | + yedges = plt.edges(y) |
| 20 | + data = state.rand(y.size, x.size) # "center" coordinates |
| 21 | + lim = (np.min(xedges), np.max(xedges)) |
| 22 | + |
| 23 | + with plt.rc.context({"cmap": "Grays", "cmap.levels": 21}): |
| 24 | + # Figure |
| 25 | + fig = plt.figure(refwidth=2.3, share=False) |
| 26 | + axs = fig.subplots(ncols=2, nrows=2) |
| 27 | + axs.format( |
| 28 | + xlabel="xlabel", |
| 29 | + ylabel="ylabel", |
| 30 | + xlim=lim, |
| 31 | + ylim=lim, |
| 32 | + xlocator=5, |
| 33 | + ylocator=5, |
| 34 | + suptitle="Standardized input demonstration", |
| 35 | + toplabels=("Coordinate centers", "Coordinate edges"), |
| 36 | + ) |
| 37 | + |
| 38 | + # Plot using both centers and edges as coordinates |
| 39 | + axs[0].pcolormesh(x, y, data) |
| 40 | + axs[1].pcolormesh(xedges, yedges, data) |
| 41 | + axs[2].contourf(x, y, data) |
| 42 | + axs[3].contourf(xedges, yedges, data) |
| 43 | + fig.show() |
| 44 | + return fig |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.mpl_image_compare |
| 48 | +def test_inbounds_data(): |
| 49 | + # Sample data |
| 50 | + cmap = "turku_r" |
| 51 | + state = np.random.RandomState(51423) |
| 52 | + N = 80 |
| 53 | + x = y = np.arange(N + 1) |
| 54 | + data = 10 + (state.normal(0, 3, size=(N, N))).cumsum(axis=0).cumsum(axis=1) |
| 55 | + xlim = ylim = (0, 25) |
| 56 | + |
| 57 | + # Plot the data |
| 58 | + fig, axs = plt.subplots( |
| 59 | + [[0, 1, 1, 0], [2, 2, 3, 3]], |
| 60 | + wratios=(1.3, 1, 1, 1.3), |
| 61 | + span=False, |
| 62 | + refwidth=2.2, |
| 63 | + ) |
| 64 | + axs[0].fill_between( |
| 65 | + xlim, |
| 66 | + *ylim, |
| 67 | + zorder=3, |
| 68 | + edgecolor="red", |
| 69 | + facecolor=plt.set_alpha("red", 0.2), |
| 70 | + ) |
| 71 | + for i, ax in enumerate(axs): |
| 72 | + inbounds = i == 1 |
| 73 | + title = f"Restricted lims inbounds={inbounds}" |
| 74 | + title += " (default)" if inbounds else "" |
| 75 | + ax.format( |
| 76 | + xlim=(None if i == 0 else xlim), |
| 77 | + ylim=(None if i == 0 else ylim), |
| 78 | + title=("Default axis limits" if i == 0 else title), |
| 79 | + ) |
| 80 | + ax.pcolor(x, y, data, cmap=cmap, inbounds=inbounds) |
| 81 | + fig.format( |
| 82 | + xlabel="xlabel", |
| 83 | + ylabel="ylabel", |
| 84 | + suptitle="Default vmin/vmax restricted to in-bounds data", |
| 85 | + ) |
| 86 | + fig.show() |
| 87 | + return fig |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.mpl_image_compare |
| 91 | +def test_colorbar(): |
| 92 | + # Sample data |
| 93 | + state = np.random.RandomState(51423) |
| 94 | + data = 10 + state.normal(0, 1, size=(33, 33)).cumsum(axis=0).cumsum(axis=1) |
| 95 | + |
| 96 | + # Figure |
| 97 | + fig, axs = plt.subplots([[1, 1, 2, 2], [0, 3, 3, 0]], ref=3, refwidth=2.3) |
| 98 | + axs.format(yformatter="none", suptitle="Discrete vs. smooth colormap levels") |
| 99 | + |
| 100 | + # Pcolor |
| 101 | + axs[0].pcolor(data, cmap="viridis", colorbar="l") |
| 102 | + axs[0].set_title("Pcolor plot\ndiscrete=True (default)") |
| 103 | + axs[1].pcolor(data, discrete=False, cmap="viridis", colorbar="r") |
| 104 | + axs[1].set_title("Pcolor plot\ndiscrete=False") |
| 105 | + |
| 106 | + # Imshow |
| 107 | + m = axs[2].imshow(data, cmap="oslo", colorbar="b") |
| 108 | + axs[2].format(title="Imshow plot\ndiscrete=False (default)", yformatter="auto") |
| 109 | + fig.show() |
| 110 | + return fig |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + for func in [ |
| 115 | + test_standardized_input, |
| 116 | + test_inbounds_data, |
| 117 | + test_colorbar, |
| 118 | + ]: |
| 119 | + fig = func() |
| 120 | + fig.savefig(f"./tests/baseline/{func.__name__}.png") |
| 121 | + plt.show(block=1) |
0 commit comments