Completed
Pull Request — master (#304)
by
unknown
01:14
created

test_plot_text_fontsize()   A

Complexity

Conditions 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
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 `station_plot` module."""
5
6
import matplotlib
7
import matplotlib.pyplot as plt
8
import numpy as np
9
import pytest
10
11
from metpy.plots import nws_layout, simple_layout, StationPlot, StationPlotLayout
12
from metpy.plots.wx_symbols import high_clouds, sky_cover
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
18
MPL_VERSION = matplotlib.__version__[:3]
19
20
21 View Code Duplication
@pytest.mark.mpl_image_compare(tolerance={'1.5': 0.04625, '1.4': 4.1}.get(MPL_VERSION, 0.0033),
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
22
                               savefig_kwargs={'dpi': 300}, remove_text=True)
23
def test_stationplot_api():
24
    """Test the StationPlot API."""
25
    fig = plt.figure(figsize=(9, 9))
26
27
    # testing data
28
    x = np.array([1, 5])
29
    y = np.array([2, 4])
30
31
    # Make the plot
32
    sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=16)
33
    sp.plot_barb([20, 0], [0, -50])
34
    sp.plot_text('E', ['KOKC', 'ICT'], color='blue')
35
    sp.plot_parameter('NW', [10.5, 15], color='red')
36
    sp.plot_symbol('S', [5, 7], high_clouds, color='green')
37
38
    sp.ax.set_xlim(0, 6)
39
    sp.ax.set_ylim(0, 6)
40
41
    return fig
42
43
44 View Code Duplication
@pytest.mark.mpl_image_compare(tolerance={'1.5': 0.05974, '1.4': 3.7}.get(MPL_VERSION, 0.0033),
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
45
                               savefig_kwargs={'dpi': 300}, remove_text=True)
46
def test_station_plot_replace():
47
    """Test that locations are properly replaced."""
48
    fig = plt.figure(figsize=(3, 3))
49
50
    # testing data
51
    x = np.array([1])
52
    y = np.array([1])
53
54
    # Make the plot
55
    sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=16)
56
    sp.plot_barb([20], [0])
57
    sp.plot_barb([5], [0])
58
    sp.plot_parameter('NW', [10.5], color='red')
59
    sp.plot_parameter('NW', [20], color='blue')
60
61
    sp.ax.set_xlim(-3, 3)
62
    sp.ax.set_ylim(-3, 3)
63
64
    return fig
65
66
67
@pytest.mark.mpl_image_compare(tolerance={'1.5': 0.036, '1.4': 2.02}.get(MPL_VERSION, 0.00321),
68
                               savefig_kwargs={'dpi': 300}, remove_text=True)
69
def test_stationlayout_api():
70
    """Test the StationPlot API."""
71
    fig = plt.figure(figsize=(9, 9))
72
73
    # testing data
74
    x = np.array([1, 5])
75
    y = np.array([2, 4])
76
    data = dict()
77
    data['temp'] = np.array([32., 212.]) * units.degF
78
    data['u'] = np.array([2, 0]) * units.knots
79
    data['v'] = np.array([0, 5]) * units.knots
80
    data['stid'] = ['KDEN', 'KSHV']
81
    data['cover'] = [3, 8]
82
83
    # Set up the layout
84
    layout = StationPlotLayout()
85
    layout.add_barb('u', 'v', units='knots')
86
    layout.add_value('NW', 'temp', fmt='0.1f', units=units.degC, color='darkred')
87
    layout.add_symbol('C', 'cover', sky_cover, color='magenta')
88
    layout.add_text((0, 2), 'stid', color='darkgrey')
89
    layout.add_value('NE', 'dewpt', color='green')  # This should be ignored
90
91
    # Make the plot
92
    sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=12)
93
    layout.plot(sp, data)
94
95
    sp.ax.set_xlim(0, 6)
96
    sp.ax.set_ylim(0, 6)
97
98
    return fig
99
100
101
def test_station_layout_odd_data():
102
    """Test more corner cases with data passed in."""
103
    fig = plt.figure(figsize=(9, 9))
104
105
    # Set up test layout
106
    layout = StationPlotLayout()
107
    layout.add_barb('u', 'v')
108
    layout.add_value('W', 'temperature', units='degF')
109
110
    # Now only use data without wind and no units
111
    data = dict(temperature=[25.])
112
113
    # Make the plot
114
    sp = StationPlot(fig.add_subplot(1, 1, 1), [1], [2], fontsize=12)
115
    layout.plot(sp, data)
116
    assert True
117
118
119
def test_station_layout_replace():
120
    """Test that layout locations are replaced."""
121
    layout = StationPlotLayout()
122
    layout.add_text('E', 'temperature')
123
    layout.add_value('E', 'dewpoint')
124
    assert 'E' in layout
125
    assert layout['E'][0] is StationPlotLayout.PlotTypes.value
126
    assert layout['E'][1] == 'dewpoint'
127
128
129
def test_station_layout_names():
130
    """Test getting station layout names."""
131
    layout = StationPlotLayout()
132
    layout.add_barb('u', 'v')
133
    layout.add_text('E', 'stid')
134
    layout.add_value('W', 'temp')
135
    layout.add_symbol('C', 'cover', lambda x: x)
136
    assert sorted(layout.names()) == ['cover', 'stid', 'temp', 'u', 'v']
137
138
139
@pytest.mark.mpl_image_compare(tolerance={'1.5': 0.05447, '1.4': 3.0}.get(MPL_VERSION, 0.0039),
140
                               savefig_kwargs={'dpi': 300}, remove_text=True)
141
def test_simple_layout():
142
    """Test metpy's simple layout for station plots."""
143
    fig = plt.figure(figsize=(9, 9))
144
145
    # testing data
146
    x = np.array([1, 5])
147
    y = np.array([2, 4])
148
    data = dict()
149
    data['air_temperature'] = np.array([32., 212.]) * units.degF
150
    data['dew_point_temperature'] = np.array([28., 80.]) * units.degF
151
    data['air_pressure_at_sea_level'] = np.array([29.92, 28.00]) * units.inHg
152
    data['eastward_wind'] = np.array([2, 0]) * units.knots
153
    data['northward_wind'] = np.array([0, 5]) * units.knots
154
    data['cloud_coverage'] = [3, 8]
155
    data['present_weather'] = [65, 75]
156
    data['unused'] = [1, 2]
157
158
    # Make the plot
159
    sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=12)
160
    simple_layout.plot(sp, data)
161
162
    sp.ax.set_xlim(0, 6)
163
    sp.ax.set_ylim(0, 6)
164
165
    return fig
166
167
168
@pytest.mark.mpl_image_compare(tolerance={'1.5': 0.1474, '1.4': 7.02}.get(MPL_VERSION, 0.0113),
169
                               savefig_kwargs={'dpi': 300}, remove_text=True)
170
def test_nws_layout():
171
    """Test metpy's NWS layout for station plots."""
172
    fig = plt.figure(figsize=(3, 3))
173
174
    # testing data
175
    x = np.array([1])
176
    y = np.array([2])
177
    data = dict()
178
    data['air_temperature'] = np.array([77]) * units.degF
179
    data['dew_point_temperature'] = np.array([71]) * units.degF
180
    data['air_pressure_at_sea_level'] = np.array([999.8]) * units('mbar')
181
    data['eastward_wind'] = np.array([15.]) * units.knots
182
    data['northward_wind'] = np.array([15.]) * units.knots
183
    data['cloud_coverage'] = [7]
184
    data['present_weather'] = [80]
185
    data['high_cloud_type'] = [1]
186
    data['medium_cloud_type'] = [3]
187
    data['low_cloud_type'] = [2]
188
    data['visibility_in_air'] = np.array([5.]) * units.mile
189
    data['tendency_of_air_pressure'] = np.array([-0.3]) * units('mbar')
190
    data['tendency_of_air_pressure_symbol'] = [8]
191
192
    # Make the plot
193
    sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=12, spacing=16)
194
    nws_layout.plot(sp, data)
195
196
    sp.ax.set_xlim(0, 3)
197
    sp.ax.set_ylim(0, 3)
198
199
    return fig
200
201
202
@pytest.mark.mpl_image_compare(tolerance={'1.4': 6.68}.get(MPL_VERSION, 1.05),
203
                               remove_text=True)
204
def test_plot_text_fontsize():
205
    """Test changing fontsize in plot_text."""
206
    fig = plt.figure(figsize=(3, 3))
207
    ax = plt.subplot(1, 1, 1)
208
209
    # testing data
210
    x = np.array([1])
211
    y = np.array([2])
212
213
    # Make the plot
214
    sp = StationPlot(ax, x, y, fontsize=36)
215
    sp.plot_text('NW', ['72'], fontsize=24)
216
    sp.plot_text('SW', ['60'], fontsize=4)
217
218
    sp.ax.set_xlim(0, 3)
219
    sp.ax.set_ylim(0, 3)
220
221
    return fig
222
223
224
def test_layout_str():
225
    """Test layout string representation."""
226
    layout = StationPlotLayout()
227
    layout.add_barb('u', 'v')
228
    layout.add_text('E', 'stid')
229
    layout.add_value('W', 'temp')
230
    layout.add_symbol('C', 'cover', lambda x: x)
231
    assert str(layout) == ('{C: (symbol, cover, ...), E: (text, stid, ...), '
232
                           'W: (value, temp, ...), barb: (barb, (\'u\', \'v\'), ...)}')
233