def test_2d_stage(): """Tests the stage() method on 2D datasets.""" fig, (empty_ax, plot_ax) = pg.subplots2d(1, 2) cosy = pg.read(datadir, 2) cosy.stage(ax=plot_ax, levels=(1e5, 1.2, 10)) # Check that the PHA was created on the right axes assert not hasattr(empty_ax, "pha") assert len(plot_ax.pha.plot_objs) == 1 # Check the properties of the plot object po = plot_ax.pha.plot_objs[0] assert po.dataset is cosy assert po.f1_bounds == "" assert po.f2_bounds == "" assert po.clevels == ([-1e5 * (1.2**i) for i in range(9, -1, -1)] + [1e5 * (1.2**i) for i in range(10)]) assert po.ccolors == ["#E8000B"] * 10 + ["#023EFF"] * 10 assert po.label is None assert np.allclose(po.f1_ppm_scale, cosy.ppm_scale(axis=0)) assert np.allclose(po.f2_ppm_scale, cosy.ppm_scale(axis=1)) assert np.allclose(po.f1_hz_scale, cosy.hz_scale(axis=0)) assert np.allclose(po.f2_hz_scale, cosy.hz_scale(axis=1)) # Check that staging a 2D dataset causes errors with pytest.raises(TypeError) as exc_info: pg.read(datadir, 1).stage(ax=plot_ax) assert "Plot queue already contains 2D spectra." in str(exc_info)
def test_1d_stage(): """Tests the stage() method on 1D datasets.""" fig, (empty_ax, plot_ax) = pg.subplots2d(1, 2) proton = pg.read(datadir, 1) proton.stage(ax=plot_ax) # Check that the PHA was created on the right axes assert not hasattr(empty_ax, "pha") assert len(plot_ax.pha.plot_objs) == 1 # Check the properties of the plot object po = plot_ax.pha.plot_objs[0] assert po.dataset is proton assert po.scale == 1 assert po.bounds == "" assert np.allclose(po.ppm_scale, proton.ppm_scale()) assert po.options["linewidth"] == 1 assert po.options["color"] == sns.color_palette("deep")[0] # It would be horribly boring to test every parameter to stage() # individually... # Stage a second dataset (well, the same thing) proton2 = pg.read(datadir, 1) proton2.stage(ax=plot_ax, scale=2, linestyle="--", label="again") assert len(plot_ax.pha.plot_objs) == 2 po2 = plot_ax.pha.plot_objs[1] assert np.allclose(po2.proc_data, po.proc_data * 2) assert np.allclose(po2.ppm_scale, po.ppm_scale) assert po2.options["linestyle"] == "--" assert po2.options["color"] == sns.color_palette("deep")[1] assert po2.options["label"] == "again" # Check dfilter. Reject any point not in [-1e4, 1e4] proton3 = pg.read(datadir, 1) proton3.stage(ax=plot_ax, dfilter=lambda t: np.abs(t) <= 1e4, bounds="2..6") assert len(plot_ax.pha.plot_objs) == 3 po3 = plot_ax.pha.plot_objs[2] # greater_equal() returns False for NaN comparisons. assert np.all( np.greater_equal(1e4, np.abs(po3.proc_data)) | np.isnan(po3.proc_data)) # Check that staging a 2D dataset causes errors with pytest.raises(TypeError) as exc_info: pg.read(datadir, 2).stage(ax=plot_ax) assert "Plot queue already contains 1D spectra." in str(exc_info)
import numpy as np import matplotlib.pyplot as plt import penguins as pg fig, axs = pg.subplots2d(2, 2) ds = pg.read(".", 2) styles = ["none", "topright", "midright", "topspin"] # Stage and construct -- this will probably be familiar for ax, style in zip(axs.flat, styles): ds.stage(ax, levels=5e5, f1_bounds="0.3..7", f2_bounds="0.3..7") pg.mkplot(ax, title=style) # Apply the styles. for ax, style in zip(axs.flat[1:], styles[1:]): pg.ymove(ax, style) # Always call cleanup_axes() after ymove()! pg.cleanup_axes() # This is not necessary in a real plot and is only included to make # it clear which plot is which. plt.subplots_adjust(hspace=0.3, wspace=0.3) pg.show()
def test_subplots2d(): """Tests that pg.subplots2d() generates figures with the correct size.""" # No arguments, should just be 4x4 fig, _ = pg.subplots2d() assert np.array_equal([4, 4], fig.get_size_inches()) # ncols, nrows passed, each should be 4x4 fig, _ = pg.subplots2d(1, 2) assert np.array_equal([8, 4], fig.get_size_inches()) fig, _ = pg.subplots2d(3, 1) assert np.array_equal([4, 12], fig.get_size_inches()) fig, _ = pg.subplots2d(5, 9) assert np.array_equal([36, 20], fig.get_size_inches()) # height_ratios passed only fig, _ = pg.subplots2d(2, 2, height_ratios=[0.5, 1]) assert np.array_equal([8, 6], fig.get_size_inches()) # wrong number of elements with pytest.raises(ValueError): fig, _ = pg.subplots2d(2, 2, height_ratios=[0.5, 1, 0.2]) # width_ratios passed only fig, _ = pg.subplots2d(2, 2, width_ratios=[0.5, 1]) assert np.array_equal([6, 8], fig.get_size_inches()) # wrong number of elements with pytest.raises(ValueError): fig, _ = pg.subplots2d(2, 2, width_ratios=[0.5, 1, 0.2]) # check that figsize overrides width_ratios fig, _ = pg.subplots2d(2, 2, width_ratios=[0.5, 1], figsize=(100, 100)) assert np.array_equal([100, 100], fig.get_size_inches()) # check that figsize overrides height_ratios fig, _ = pg.subplots2d(2, 2, height_ratios=[0.5, 1], figsize=(100, 100)) assert np.array_equal([100, 100], fig.get_size_inches()) # check that figsize overrides both of these fig, _ = pg.subplots2d(2, 2, width_ratios=[0.5, 1], height_ratios=[0.5, 1], figsize=(100, 100)) assert np.array_equal([100, 100], fig.get_size_inches())