Skip to content

Commit

Permalink
add more layer options for mappanel
Browse files Browse the repository at this point in the history
  • Loading branch information
kgoebber committed Aug 31, 2023
1 parent ae3e365 commit c16fef1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
60 changes: 55 additions & 5 deletions src/metpy/plots/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def plot_kwargs(data):
return kwargs


def get_cartopy_color(val):

Check failure on line 69 in src/metpy/plots/declarative.py

View workflow job for this annotation

GitHub Actions / Flake8

[flake8] reported by reviewdog 🐶 D103 Missing docstring in public function Raw Output: src/metpy/plots/declarative.py:69:5: D103 Missing docstring in public function
from cartopy.feature import COLORS
return COLORS[val]


class ValidationMixin:
"""Provides validation of attribute names when set by user."""

Expand Down Expand Up @@ -267,9 +272,38 @@ class MapPanel(Panel, ValidationMixin):
layers_linewidth = List(Union([Int(), Float()], allow_none=True), default_value=[1])
layers_linewidth.__doc__ = """A list of values defining the linewidth for a layer.
An option to set a different color for the map layer edge colors. Length of list should
match that of layers if not using default value. Behavior is to repeat colors if not enough
provided by user. Use `None` value for 'ocean', 'lakes', 'rivers', and 'land'.
An option to set a different linewidth for the layer feature. Length of list should
match that of layers if not using default value. Behavior is to repeat linewidth if
not enough provided by user. Use `None` value for 'ocean', 'lakes', 'rivers', and 'land'.
"""

layers_linestyle = List(Unicode(), default_value=['solid'])
layers_linestyle.__doc__ = """A list of string values defining the linestyle for a layer or
None.
Default is `solid`, which, will use a solid lines for drawing the layer. Behavior is to
repeat linestyle if not enough provided by user.
The valid string values are those of Matplotlib which are 'solid', 'dashed', 'dotted', and
'dashdot', as well as their short codes ('-', '--', '.', '-.'). The object `None`, as
described above, can also be used. Use `None` value for 'ocean', 'lakes', 'rivers', and
'land'.
"""

layers_zorder = List(Union([Int(), Float()], allow_none=True), default_value=[None])
layers_zorder.__doc__ = """A list of values defining the zorder for a layer.
An option to set a different zorder for the map layer edge colors. Length of list should
match that of layers if not using default value. Behavior is to repeat zorder if not enough
provided by user.
"""

layers_alpha = List(Union([Int(), Float()], allow_none=True), default_value=[1])
layers_alpha.__doc__ = """A list of values defining the alpha for a layer.
An option to set a different alpha for the map layer edge colors. Length of list should
match that of layers if not using default value. Behavior is to repeat alpha if not enough
provided by user.
"""

title = Unicode()
Expand Down Expand Up @@ -465,13 +499,29 @@ def draw(self):
self.layers_edgecolor *= len(self.layers)
if len(self.layers) > len(self.layers_linewidth):
self.layers_linewidth *= len(self.layers)
if len(self.layers) > len(self.layers_linestyle):
self.layers_linestyle *= len(self.layers)
if len(self.layers) > len(self.layers_zorder):
self.layers_zorder *= len(self.layers)
if len(self.layers) > len(self.layers_alpha):
self.layers_alpha *= len(self.layers)
for i, feat in enumerate(self._layer_features):
if self.layers[i] in ['', 'land', 'lake', 'river']:
if self.layers[i] in ['', 'land', 'lakes', 'ocean']:
color = 'face'
else:
color = self.layers_edgecolor[i]
if self.layers_edgecolor[i] in ['water', 'land', 'land_alt1']:
color = get_cartopy_color(self.layers_edgecolor[i])
width = self.layers_linewidth[i]
self.ax.add_feature(feat, edgecolor=color, linewidth=width)
style = self.layers_linestyle[i]
zorder = self.layers_zorder[i]
alpha = self.layers_alpha[i]
if zorder is not None:
self.ax.add_feature(feat, edgecolor=color, linewidth=width,
linestyle=style, zorder=zorder, alpha=alpha)
else:
self.ax.add_feature(feat, edgecolor=color, linewidth=width,
linestyle=style, alpha=alpha)

# Use the set title or generate one.
if (self.right_title is None) and (self.left_title is None):
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 34 additions & 1 deletion tests/plots/test_declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def test_declarative_contour_options():
@pytest.mark.mpl_image_compare(remove_text=True, tolerance=0.082)
@needs_cartopy
def test_declarative_layers_plot_options():
"""Test making a contour plot."""
"""Test declarative layer options of edgecolor and linewidth."""
data = xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False))

contour = ContourPlot()
Expand All @@ -394,6 +394,39 @@ def test_declarative_layers_plot_options():
return pc.figure


@pytest.mark.mpl_image_compare(remove_text=True, tolerance=0.0)
@needs_cartopy
def test_declarative_additional_layers_plot_options():
"""Test additional declarative layer options of linestyle, zorder, and alpha."""
data = xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False))

contour = ContourPlot()
contour.data = data
contour.field = 'Temperature'
contour.level = 700 * units.hPa
contour.contours = 5
contour.linewidth = 1
contour.linecolor = 'grey'

panel = MapPanel()
panel.area = 'us'
panel.projection = 'lcc'
panel.layers = ['coastline', 'usstates', 'borders', 'lakes', 'rivers']
panel.layers_edgecolor = ['blue', 'red', 'black', None, 'water']
panel.layers_linewidth = [0.75, 0.75, 1, 1, 1]
panel.layers_linestyle = ['solid', 'dotted', 'dashed', 'dotted']
panel.layers_alpha = [1, .5, .75, 1]
panel.layers_zorder = [1, 1, 1, -1, -1]
panel.plots = [contour]

pc = PanelContainer()
pc.size = (8, 8)
pc.panels = [panel]
pc.draw()

return pc.figure


@pytest.mark.mpl_image_compare(remove_text=True, tolerance=0.025)
@needs_cartopy
def test_declarative_contour_convert_units():
Expand Down

0 comments on commit c16fef1

Please sign in to comment.