Completed
Push — master ( 323290...ff6f72 )
by Ryan
18s
created

test_hodograph_plot_layers_different_units()   A

Complexity

Conditions 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
dl 16
loc 16
rs 9.4285
1
# Copyright (c) 2015,2016,2017 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
import matplotlib
7
from matplotlib.gridspec import GridSpec
8
import matplotlib.pyplot as plt
9
import numpy as np
10
import pytest
11
12
from metpy.plots import Hodograph, SkewT
13
# Fixtures to make sure we have the right backend and consistent round
14
from metpy.testing import patch_round, set_agg_backend  # noqa: F401
15
from metpy.units import units
16
17
MPL_VERSION = matplotlib.__version__[:3]
18
19
20
@pytest.mark.mpl_image_compare(tolerance=0.021, remove_text=True)
21
def test_skewt_api():
22
    """Test the SkewT API."""
23
    fig = plt.figure(figsize=(9, 9))
24
    skew = SkewT(fig)
25
26
    # Plot the data using normal plotting functions, in this case using
27
    # log scaling in Y, as dictated by the typical meteorological plot
28
    p = np.linspace(1000, 100, 10)
29
    t = np.linspace(20, -20, 10)
30
    u = np.linspace(-10, 10, 10)
31
    skew.plot(p, t, 'r')
32
    skew.plot_barbs(p, u, u)
33
34
    # Add the relevant special lines
35
    skew.plot_dry_adiabats()
36
    skew.plot_moist_adiabats()
37
    skew.plot_mixing_lines()
38
39
    return fig
40
41
42
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
43
def test_skewt_subplot():
44
    """Test using SkewT on a sub-plot."""
45
    fig = plt.figure(figsize=(9, 9))
46
    SkewT(fig, subplot=(2, 2, 1))
47
    return fig
48
49
50
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
51
def test_skewt_gridspec():
52
    """Test using SkewT on a sub-plot."""
53
    fig = plt.figure(figsize=(9, 9))
54
    gs = GridSpec(1, 2)
55
    SkewT(fig, subplot=gs[0, 1])
56
    return fig
57
58
59
def test_skewt_with_grid_enabled():
60
    """Test using SkewT when gridlines are already enabled (#271)."""
61
    with plt.rc_context(rc={'axes.grid': True}):
62
        # Also tests when we don't pass in Figure
63
        SkewT()
64
65
66
@pytest.fixture()
67
def test_profile():
68
    """Return data for a test profile."""
69
    return np.linspace(1000, 100, 10), np.linspace(20, -20, 10), np.linspace(25, -30, 10)
70
71
72 View Code Duplication
@pytest.mark.mpl_image_compare(tolerance={'1.4': 1.71}.get(MPL_VERSION, 0.), remove_text=True)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
73
def test_skewt_shade_cape_cin(test_profile):
74
    """Test shading CAPE and CIN on a SkewT plot."""
75
    p, t, tp = test_profile
76
    fig = plt.figure(figsize=(9, 9))
77
    skew = SkewT(fig)
78
    skew.plot(p, t, 'r')
79
    skew.plot(p, tp, 'k')
80
    skew.shade_cape(p, t, tp)
81
    skew.shade_cin(p, t, tp)
82
    skew.ax.set_xlim(-50, 50)
83
    return fig
84
85
86 View Code Duplication
@pytest.mark.mpl_image_compare(tolerance={'1.4': 1.70}.get(MPL_VERSION, 0.), remove_text=True)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
87
def test_skewt_shade_area(test_profile):
88
    """Test shading areas on a SkewT plot."""
89
    p, t, tp = test_profile
90
    fig = plt.figure(figsize=(9, 9))
91
    skew = SkewT(fig)
92
    skew.plot(p, t, 'r')
93
    skew.plot(p, tp, 'k')
94
    skew.shade_area(p, t, tp)
95
    skew.ax.set_xlim(-50, 50)
96
    return fig
97
98
99
def test_skewt_shade_area_invalid(test_profile):
100
    """Test shading areas on a SkewT plot."""
101
    p, t, tp = test_profile
102
    fig = plt.figure(figsize=(9, 9))
103
    skew = SkewT(fig)
104
    skew.plot(p, t, 'r')
105
    skew.plot(p, tp, 'k')
106
    with pytest.raises(ValueError):
107
        skew.shade_area(p, t, tp, which='positve')
108
109
110 View Code Duplication
@pytest.mark.mpl_image_compare(tolerance={'1.4': 1.75}.get(MPL_VERSION, 0.), remove_text=True)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
111
def test_skewt_shade_area_kwargs(test_profile):
112
    """Test shading areas on a SkewT plot with kwargs."""
113
    p, t, tp = test_profile
114
    fig = plt.figure(figsize=(9, 9))
115
    skew = SkewT(fig)
116
    skew.plot(p, t, 'r')
117
    skew.plot(p, tp, 'k')
118
    skew.shade_area(p, t, tp, facecolor='m')
119
    skew.ax.set_xlim(-50, 50)
120
    return fig
121
122
123
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
124
def test_hodograph_api():
125
    """Basic test of Hodograph API."""
126
    fig = plt.figure(figsize=(9, 9))
127
    ax = fig.add_subplot(1, 1, 1)
128
    hodo = Hodograph(ax, component_range=60)
129
    hodo.add_grid(increment=5, color='k')
130
    hodo.plot([1, 10], [1, 10], color='red')
131
    hodo.plot_colormapped(np.array([1, 3, 5, 10]), np.array([2, 4, 6, 11]),
132
                          np.array([0.1, 0.3, 0.5, 0.9]), cmap='Greys')
133
    return fig
134
135
136
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
137
def test_hodograph_units():
138
    """Test passing unit-ed quantities to Hodograph."""
139
    fig = plt.figure(figsize=(9, 9))
140
    ax = fig.add_subplot(1, 1, 1)
141
    hodo = Hodograph(ax)
142
    u = np.arange(10) * units.kt
143
    v = np.arange(10) * units.kt
144
    hodo.plot(u, v)
145
    hodo.plot_colormapped(u, v, np.sqrt(u * u + v * v), cmap='Greys')
146
    ax.set_xlabel('')
147
    ax.set_ylabel('')
148
    return fig
149
150
151
def test_hodograph_alone():
152
    """Test to create Hodograph without specifying axes."""
153
    Hodograph()
154
155
156
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
157
def test_hodograph_plot_colormapped():
158
    """Test hodograph colored line with NaN values."""
159
    u = np.arange(5., 65., 5)
160
    v = np.arange(-5., -65., -5)
161
    u[3] = np.nan
162
    v[6] = np.nan
163
    fig = plt.figure(figsize=(9, 9))
164
    ax = fig.add_subplot(1, 1, 1)
165
    hodo = Hodograph(ax, component_range=80)
166
    hodo.add_grid(increment=20, color='k')
167
    hodo.plot_colormapped(u, v, np.hypot(u, v), cmap='Greys')
168
169
    return fig
170
171
172
@pytest.mark.mpl_image_compare(tolerance=0.021, remove_text=True)
173
def test_skewt_barb_color():
174
    """Test plotting colored wind barbs on the Skew-T."""
175
    fig = plt.figure(figsize=(9, 9))
176
    skew = SkewT(fig)
177
178
    p = np.linspace(1000, 100, 10)
179
    u = np.linspace(-10, 10, 10)
180
    skew.plot_barbs(p, u, u, c=u)
181
182
    return fig
183
184
185 View Code Duplication
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
186
def test_hodograph_plot_layers():
187
    """Test hodograph colored height layers with interpolation."""
188
    u = np.zeros((6)) * units.knots
189
    v = np.array([0, 10, 20, 30, 40, 50]) * units.knots
190
    heights = np.array([0, 1000, 2000, 3000, 4000, 5000]) * units.m
191
    bounds = np.array([500, 1500, 2500, 3500, 4500]) * units.m
192
    colors = ['r', 'g', 'b', 'r']
193
    fig = plt.figure(figsize=(7, 7))
194
    ax1 = fig.add_subplot(1, 1, 1)
195
    h = Hodograph(ax1)
196
    h.add_grid(increment=10)
197
    h.plot_colormapped(u, v, heights, colors=colors, bounds=bounds)
198
    ax1.set_xlim(-50, 50)
199
    ax1.set_ylim(-5, 50)
200
201
    return fig
202
203
204 View Code Duplication
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
205
def test_hodograph_plot_layers_different_units():
206
    """Test hodograph colored height layers with interpolation and different units."""
207
    u = np.zeros((6)) * units.knots
208
    v = np.array([0, 10, 20, 30, 40, 50]) * units.knots
209
    heights = np.array([0, 1, 2, 3, 4, 5]) * units.km
210
    bounds = np.array([500, 1500, 2500, 3500, 4500]) * units.m
211
    colors = ['r', 'g', 'b', 'r']
212
    fig = plt.figure(figsize=(7, 7))
213
    ax1 = fig.add_subplot(1, 1, 1)
214
    h = Hodograph(ax1)
215
    h.add_grid(increment=10)
216
    h.plot_colormapped(u, v, heights, colors=colors, bounds=bounds)
217
    ax1.set_xlim(-50, 50)
218
    ax1.set_ylim(-5, 50)
219
    return fig
220
221
222
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
223
def test_hodograph_plot_arbitrary_layer():
224
    """Test hodograph colored layers for arbitrary variables without interpolation."""
225
    u = np.arange(5, 65, 5) * units('knot')
226
    v = np.arange(-5, -65, -5) * units('knot')
227
    speed = np.sqrt(u ** 2 + v ** 2)
228
    colors = ['red', 'green', 'blue']
229
    levels = [0, 10, 20, 30] * units('knot')
230
    fig = plt.figure(figsize=(9, 9))
231
    ax = fig.add_subplot(1, 1, 1)
232
    hodo = Hodograph(ax, component_range=80)
233
    hodo.add_grid(increment=20, color='k')
234
    hodo.plot_colormapped(u, v, speed, bounds=levels, colors=colors)
235
236
    return fig
237