test_regression.test_plot_with_panels()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 43
rs 9.184
c 0
b 0
f 0
cc 3
nop 0
1
from re import A
2
import matplotlib.pyplot as plt
3
import numpy as np
4
import pytest
5
6
import sacredfig
7
8
from sacredfig import add_labels_to_axes
9
from sacredfig import cm2in
10
11
12
@pytest.mark.mpl_image_compare
13
def test_plot_with_panels():
14
    with plt.style.context(sacredfig.style):
15
        fig, axes = plt.subplots(1, 2, figsize=(12 * cm2in, 6 * cm2in), dpi=150)
16
17
        # Panel A
18
        x = np.linspace(0, 5, 501)
19
        y = x * np.sin(x * np.pi)
20
        z = -(x + 0.5) * np.sin((x + 0.5) * np.pi)
21
        axes[0].plot(x, y)
22
        axes[0].plot(x, z, alpha=0.5)
23
        axes[0].fill_between(
24
            x,
25
            y,
26
            z,
27
            where=y > z,
28
            facecolor="#eeeeee",
29
        )
30
        axes[0].set(
31
            xlim=(0, 5), ylim=(-6, 6), xlabel="Time $t$", ylabel="Perturbations ($Y_t$, $Z_t$)"
32
        )
33
        axes[0].grid(False, which="major", axis="x")
34
35
        # Panel B
36
        axes[1].plot(y, z, c="k")
37
        axes[1].set(
38
            xlim=(-6, 6),
39
            ylim=(-6, 6),
40
            xlabel="Y-axis perturbation ($Y_t$)",
41
            ylabel="Z-axis perturbation ($Z_t$)",
42
        )
43
44
        # Ensure each panel is a perfect square
45
        for ax in axes:
46
            ax.set_box_aspect(1)
47
48
        # Adjust space between subplots
49
        fig.subplots_adjust(wspace=0.3)
50
51
        # Add labels a. and b.
52
        add_labels_to_axes(fig, axes)
53
54
        return fig
55
56
57
@pytest.mark.mpl_image_compare
58
def test_plot_boxplot():
59
    import seaborn as sns
60
    sns.reset_orig()
61
62
    iris = sns.load_dataset("iris")
63
64
    with plt.style.context(sacredfig.style):
65
66
        fig, ax = plt.subplots(figsize=(6 * cm2in, 6 * cm2in), dpi=150)
67
        ax.grid(False, which='major', axis='x')
68
69
        ax.boxplot([iris.sepal_length.values, iris.sepal_width.values],
70
                labels=['Sepal length', 'Sepal width'])
71
72
        ax.set_box_aspect(1)
73
        ax.set(xlabel="Attribute", ylabel="Empirical distribution", ylim=(0, 10));
74
75
        return fig
76
77
@pytest.mark.mpl_image_compare
78
def test_plot_boxplot_seaborn():
79
    from sacredfig.seaborn import boxplot, stripplot
80
81
    import seaborn as sns
82
    sns.reset_orig()
83
    plt.style.use(sacredfig.style)
84
85
    iris = sns.load_dataset("iris")
86
87
    fig, ax = plt.subplots(figsize=(6 * cm2in, 6 * cm2in), dpi=150)
88
    boxplot(data=iris, ax=ax, width=0.4, fliersize=0)
89
90
    np.random.seed(0)
91
    stripplot(data=iris, ax=ax)
92
93
    ax.set(ylim=(0, 8))
94
    ax.set_box_aspect(1)
95
96
    fig.autofmt_xdate()
97
    return fig
98