Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more layer options for mappanel #3174

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 54 additions & 7 deletions src/metpy/plots/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ def plot_kwargs(data, args):
return kwargs


def get_cartopy_color(val):
"""Provide the special map feature colors from Cartopy."""
from cartopy.feature import COLORS
return COLORS[val]


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

Expand Down Expand Up @@ -281,9 +287,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 @@ -479,13 +514,25 @@ 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']:
color = self.layers_edgecolor[i]
if self.layers[i] in ['', 'land', '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]
kwargs = {'zorder': zorder} if zorder is not None else {}
self.ax.add_feature(feat, edgecolor=color, linewidth=width,
linestyle=style, alpha=alpha, **kwargs)

# 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.
Binary file modified tests/plots/baseline/test_declarative_barb_options.png
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 @@ -368,7 +368,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 @@ -395,6 +395,39 @@ def test_declarative_layers_plot_options():
return pc.figure


@pytest.mark.mpl_image_compare(remove_text=True, tolerance=0.021)
@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=2.74 if mpl_version_before('3.8') else 1.91)
@needs_cartopy
Expand Down