Skip to content

Commit

Permalink
Merge pull request #304 from jrleeman/SymbolGallery
Browse files Browse the repository at this point in the history
Symbol Gallery
  • Loading branch information
dopplershift committed Feb 13, 2017
2 parents dfe6132 + ba4b896 commit 414dea5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
7 changes: 6 additions & 1 deletion docs/conf.py
Expand Up @@ -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 = {
Expand All @@ -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

Expand Down
59 changes: 58 additions & 1 deletion metpy/plots/station_plot.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions metpy/plots/tests/test_station_plot.py
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions metpy/plots/tests/test_wx_symbols.py
Expand Up @@ -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
4 changes: 4 additions & 0 deletions metpy/plots/wx_symbols.py
Expand Up @@ -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.
Expand Down

0 comments on commit 414dea5

Please sign in to comment.