diff --git a/docs/conf.py b/docs/conf.py index 59b0475257b..7d77c7feb47 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -40,7 +40,8 @@ 'sphinx.ext.intersphinx', 'sphinx.ext.mathjax', 'sphinx.ext.napoleon', - 'sphinx_gallery.gen_gallery' + 'sphinx_gallery.gen_gallery', + 'matplotlib.sphinxext.plot_directive' ] sphinx_gallery_conf = { @@ -56,6 +57,10 @@ 'mod_example_dir': 'api/generated' } +# Turn off code and image links for embedded mpl plots +plot_html_show_source_link = False +plot_html_show_formats = False + mathjax_path = 'https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' # autosummary_generate = True diff --git a/metpy/plots/station_plot.py b/metpy/plots/station_plot.py index 9d41822f179..9543c5c6f48 100644 --- a/metpy/plots/station_plot.py +++ b/metpy/plots/station_plot.py @@ -90,6 +90,62 @@ def plot_symbol(self, location, codes, symbol_mapper, **kwargs): Additional keyword arguments to use for matplotlib's plotting functions. + .. plot:: + + import matplotlib.pyplot as plt + import numpy as np + from math import ceil + + from metpy.plots import StationPlot + from metpy.plots.wx_symbols import current_weather, current_weather_auto + from metpy.plots.wx_symbols import low_clouds, mid_clouds, high_clouds + from metpy.plots.wx_symbols import sky_cover, pressure_tendency + + + def plot_symbols(mapper, name, nwrap=12, figsize=(10, 1.4)): + + # Determine how many symbols there are and layout in rows of nwrap + # if there are more than nwrap symbols + num_symbols = len(mapper) + codes = np.arange(len(mapper)) + ncols = nwrap + if num_symbols <= nwrap: + nrows = 1 + x = np.linspace(0, 1, len(mapper)) + y = np.ones_like(x) + ax_height = 0.8 + else: + nrows = int(ceil(num_symbols / ncols)) + x = np.tile(np.linspace(0, 1, ncols), nrows)[:num_symbols] + y = np.repeat(np.arange(nrows, 0, -1), ncols)[:num_symbols] + figsize = (10, 1 * nrows + 0.4) + ax_height = 0.8 + 0.018 * nrows + + fig = plt.figure(figsize=figsize, dpi=300) + ax = fig.add_axes([0, 0, 1, ax_height]) + ax.set_title(name, size=20) + ax.xaxis.set_ticks([]) + ax.yaxis.set_ticks([]) + ax.set_frame_on(False) + + # Plot + sp = StationPlot(ax, x, y, fontsize=36) + sp.plot_symbol('C', codes, mapper) + sp.plot_parameter((0, -1), codes, fontsize=18) + + ax.set_ylim(-0.05, nrows + 0.5) + + plt.show() + + + plot_symbols(current_weather, "Current Weather Symbols") + plot_symbols(current_weather_auto, "Current Weather Auto Reported Symbols") + plot_symbols(low_clouds, "Low Cloud Symbols") + plot_symbols(mid_clouds, "Mid Cloud Symbols") + plot_symbols(high_clouds, "High Cloud Symbols") + plot_symbols(sky_cover, "Sky Cover Symbols") + plot_symbols(pressure_tendency, "Pressure Tendency Symbols") + See Also -------- plot_barb, plot_parameter, plot_text @@ -167,7 +223,8 @@ def plot_text(self, location, text, **kwargs): kwargs['transform'] = self.transform text_collection = self.ax.scattertext(self.x, self.y, text, loc=location, - size=self.fontsize, **kwargs) + size=kwargs.pop('fontsize', self.fontsize), + **kwargs) if location in self.items: self.items[location].remove() self.items[location] = text_collection diff --git a/metpy/plots/tests/baseline/test_plot_text_fontsize.png b/metpy/plots/tests/baseline/test_plot_text_fontsize.png new file mode 100644 index 00000000000..7443afc4e2f Binary files /dev/null and b/metpy/plots/tests/baseline/test_plot_text_fontsize.png differ diff --git a/metpy/plots/tests/test_station_plot.py b/metpy/plots/tests/test_station_plot.py index 33fd0c14469..44d1fcd1d81 100644 --- a/metpy/plots/tests/test_station_plot.py +++ b/metpy/plots/tests/test_station_plot.py @@ -199,6 +199,28 @@ def test_nws_layout(): return fig +@pytest.mark.mpl_image_compare(tolerance={'1.4': 6.68}.get(MPL_VERSION, 1.05), + remove_text=True) +def test_plot_text_fontsize(): + """Test changing fontsize in plot_text.""" + fig = plt.figure(figsize=(3, 3)) + ax = plt.subplot(1, 1, 1) + + # testing data + x = np.array([1]) + y = np.array([2]) + + # Make the plot + sp = StationPlot(ax, x, y, fontsize=36) + sp.plot_text('NW', ['72'], fontsize=24) + sp.plot_text('SW', ['60'], fontsize=4) + + sp.ax.set_xlim(0, 3) + sp.ax.set_ylim(0, 3) + + return fig + + def test_layout_str(): """Test layout string representation.""" layout = StationPlotLayout() diff --git a/metpy/plots/tests/test_wx_symbols.py b/metpy/plots/tests/test_wx_symbols.py index c7bfd47149f..8a64a01d621 100644 --- a/metpy/plots/tests/test_wx_symbols.py +++ b/metpy/plots/tests/test_wx_symbols.py @@ -17,3 +17,8 @@ def test_alt_char(): """Test alternate character functionality for mapper.""" assert current_weather.alt_char(7, 1) == u'\ue9a6' assert current_weather.alt_char(7, 2) == u'\ue9a7' + + +def test_mapper_len(): + """Test getting the length of the mapper.""" + assert len(current_weather) == 100 diff --git a/metpy/plots/wx_symbols.py b/metpy/plots/wx_symbols.py index 79dcf743be2..5eac18fce8a 100644 --- a/metpy/plots/wx_symbols.py +++ b/metpy/plots/wx_symbols.py @@ -75,6 +75,10 @@ def __call__(self, code): """Return the Unicode code point corresponding to `code`.""" return self.chrs[code] + def __len__(self): + """Return the number of codes supported by this mapping.""" + return len(self.chrs) + def alt_char(self, code, alt): """Get one of the alternate code points for a given value.