summaryrefslogtreecommitdiff
path: root/pint/testsuite/test_matplotlib.py
blob: 0735721c08e64d44a78445556e5d80c8e43e777a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pytest

from pint import UnitRegistry

# Conditionally import matplotlib and NumPy
plt = pytest.importorskip("matplotlib.pyplot", reason="matplotlib is not available")
np = pytest.importorskip("numpy", reason="NumPy is not available")


@pytest.fixture(scope="module")
def local_registry():
    # Set up unit registry for matplotlib
    ureg = UnitRegistry()
    ureg.setup_matplotlib(True)
    return ureg


# Set up matplotlib
plt.switch_backend("agg")


@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
def test_basic_plot(local_registry):
    y = np.linspace(0, 30) * local_registry.miles
    x = np.linspace(0, 5) * local_registry.hours

    fig, ax = plt.subplots()
    ax.plot(x, y, "tab:blue")
    ax.axhline(26400 * local_registry.feet, color="tab:red")
    ax.axvline(120 * local_registry.minutes, color="tab:green")

    return fig


@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
def test_plot_with_set_units(local_registry):
    y = np.linspace(0, 30) * local_registry.miles
    x = np.linspace(0, 5) * local_registry.hours

    fig, ax = plt.subplots()
    ax.yaxis.set_units(local_registry.inches)
    ax.xaxis.set_units(local_registry.seconds)

    ax.plot(x, y, "tab:blue")
    ax.axhline(26400 * local_registry.feet, color="tab:red")
    ax.axvline(120 * local_registry.minutes, color="tab:green")

    return fig


@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
def test_plot_with_non_default_format(local_registry):
    local_registry.mpl_formatter = "{:~P}"

    y = np.linspace(0, 30) * local_registry.miles
    x = np.linspace(0, 5) * local_registry.hours

    fig, ax = plt.subplots()
    ax.yaxis.set_units(local_registry.inches)
    ax.xaxis.set_units(local_registry.seconds)

    ax.plot(x, y, "tab:blue")
    ax.axhline(26400 * local_registry.feet, color="tab:red")
    ax.axvline(120 * local_registry.minutes, color="tab:green")

    return fig