Completed
Pull Request — master (#282)
by Ryan
01:37
created

test_skewt_with_grid_enabled()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
1
# Copyright (c) 2008-2015 MetPy Developers.
2
# Distributed under the terms of the BSD 3-Clause License.
3
# SPDX-License-Identifier: BSD-3-Clause
4
"""Tests for the `skewt` module."""
5
6
from matplotlib.gridspec import GridSpec
7
import matplotlib.pyplot as plt
8
import numpy as np
9
import pytest
10
11
from metpy.plots import Hodograph, SkewT
12
# Fixtures to make sure we have the right backend and consistent round
13
from metpy.testing import patch_round, set_agg_backend  # noqa: F401
14
from metpy.units import units
15
16
17
@pytest.mark.mpl_image_compare(tolerance=0.021, remove_text=True)
18
def test_skewt_api():
19
    """Test the SkewT API."""
20
    fig = plt.figure(figsize=(9, 9))
21
    skew = SkewT(fig)
22
23
    # Plot the data using normal plotting functions, in this case using
24
    # log scaling in Y, as dictated by the typical meteorological plot
25
    p = np.linspace(1000, 100, 10)
26
    t = np.linspace(20, -20, 10)
27
    u = np.linspace(-10, 10, 10)
28
    skew.plot(p, t, 'r')
29
    skew.plot_barbs(p, u, u)
30
31
    # Add the relevant special lines
32
    skew.plot_dry_adiabats()
33
    skew.plot_moist_adiabats()
34
    skew.plot_mixing_lines()
35
36
    return fig
37
38
39
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
40
def test_skewt_subplot():
41
    """Test using SkewT on a sub-plot."""
42
    fig = plt.figure(figsize=(9, 9))
43
    SkewT(fig, subplot=(2, 2, 1))
44
    return fig
45
46
47
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
48
def test_skewt_gridspec():
49
    """Test using SkewT on a sub-plot."""
50
    fig = plt.figure(figsize=(9, 9))
51
    gs = GridSpec(1, 2)
52
    SkewT(fig, subplot=gs[0, 1])
53
    return fig
54
55
56
def test_skewt_with_grid_enabled():
57
    """Test using SkewT when gridlines are already enabled (#271)."""
58
    with plt.rc_context(rc={'axes.grid': True}):
59
        # Also tests when we don't pass in Figure
60
        SkewT()
61
62
63
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
64
def test_hodograph_api():
65
    """Basic test of Hodograph API."""
66
    fig = plt.figure(figsize=(9, 9))
67
    ax = fig.add_subplot(1, 1, 1)
68
    hodo = Hodograph(ax, component_range=60)
69
    hodo.add_grid(increment=5, color='k')
70
    hodo.plot([1, 10], [1, 10], color='red')
71
    hodo.plot_colormapped(np.array([1, 3, 5, 10]), np.array([2, 4, 6, 11]),
72
                          np.array([0.1, 0.3, 0.5, 0.9]), cmap='Greys')
73
    return fig
74
75
76
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
77
def test_hodograph_units():
78
    """Test passing unit-ed quantities to Hodograph."""
79
    fig = plt.figure(figsize=(9, 9))
80
    ax = fig.add_subplot(1, 1, 1)
81
    hodo = Hodograph(ax)
82
    u = np.arange(10) * units.kt
83
    v = np.arange(10) * units.kt
84
    hodo.plot(u, v)
85
    hodo.plot_colormapped(u, v, np.sqrt(u * u + v * v), cmap='Greys')
86
    return fig
87
88
89
def test_hodograph_alone():
90
    """Test to create Hodograph without specifying axes."""
91
    Hodograph()
92