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 declarative layer attributes control #2425

Merged
merged 2 commits into from
Apr 11, 2022
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
29 changes: 27 additions & 2 deletions src/metpy/plots/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,22 @@ class MapPanel(Panel, ValidationMixin):
Cartopy Feature objects.
"""

layers_edgecolor = List(Unicode(allow_none=True), default_value=['black'])
layers_edgecolor.__doc__ = """A list of strings for a pre-defined edgecolor 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'.
"""

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'.
"""

title = Unicode()
title.__doc__ = """A string to set a title for the figure.

Expand Down Expand Up @@ -866,8 +882,17 @@ def draw(self):
p.draw()

# Add all of the maps
for feat in self._layer_features:
self.ax.add_feature(feat)
if len(self.layers) > len(self.layers_edgecolor):
self.layers_edgecolor *= len(self.layers)
if len(self.layers) > len(self.layers_linewidth):
self.layers_linewidth *= len(self.layers)
for i, feat in enumerate(self._layer_features):
if self.layers[i] in ['', 'land', 'lake', 'river']:
color = 'face'
else:
color = self.layers_edgecolor[i]
width = self.layers_linewidth[i]
self.ax.add_feature(feat, edgecolor=color, linewidth=width)

# Use the set title or generate one.
title = self.title or ',\n'.join(plot.name for plot in self.plots)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions tests/plots/test_declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,36 @@ def test_declarative_contour_options():
return pc.figure


@pytest.mark.mpl_image_compare(remove_text=True, tolerance=0.08)
@needs_cartopy
def test_declarative_layers_plot_options():
"""Test making a contour plot."""
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']
panel.layers_edgecolor = ['blue', 'red', 'black']
panel.layers_linewidth = [0.75, 0.75, 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.0188)
@needs_cartopy
def test_declarative_contour_convert_units():
Expand Down