summaryrefslogtreecommitdiff
path: root/pint/testsuite/test_matplotlib.py
blob: 3c590ae2758650a35f59520d23d8bbb59ca8ff8d (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
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")

# Set up unit registry for matplotlib
ureg = UnitRegistry()
ureg.setup_matplotlib(True)

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


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

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

    return fig


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

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

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

    return fig