Skip to content

Commit

Permalink
add left and right title option (#2645)
Browse files Browse the repository at this point in the history
  • Loading branch information
kgoebber authored Sep 16, 2022
1 parent 01f4e2d commit eb0efa1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/metpy/plots/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,20 @@ class MapPanel(Panel, ValidationMixin):
This trait sets a user-defined title that will plot at the top center of the figure.
"""

left_title = Unicode(allow_none=True, default_value=None)
left_title.__doc__ = """A string to set a title for the figure with the location on the
top left of the figure.
This trait sets a user-defined title that will plot at the top left of the figure.
"""

right_title = Unicode(allow_none=True, default_value=None)
right_title.__doc__ = """A string to set a title for the figure with the location on the
top right of the figure.
This trait sets a user-defined title that will plot at the top right of the figure.
"""

title_fontsize = Union([Int(), Float(), Unicode()], allow_none=True, default_value=None)
title_fontsize.__doc__ = """An integer or string value for the font size of the title of
the figure.
Expand Down Expand Up @@ -896,8 +910,18 @@ def draw(self):
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)
self.ax.set_title(title, fontsize=self.title_fontsize)
if (self.right_title is None) and (self.left_title is None):
title = self.title or ',\n'.join(plot.name for plot in self.plots)
self.ax.set_title(title, fontsize=self.title_fontsize)
else:
if self.title is not None:
self.ax.set_title(self.title, fontsize=self.title_fontsize)
if self.right_title is not None:
self.ax.set_title(self.right_title, fontsize=self.title_fontsize,
loc='right')
if self.left_title is not None:
self.ax.set_title(self.left_title, fontsize=self.title_fontsize,
loc='left')
self._need_redraw = False

def __copy__(self):
Expand Down
Binary file added tests/plots/baseline/test_declarative_titles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions tests/plots/test_declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,37 @@ def test_declarative_contour():
return pc.figure


@pytest.mark.mpl_image_compare(remove_text=False, tolerance=0)
@needs_cartopy
def test_declarative_titles():
"""Test making a contour plot with multiple titles."""
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 = 30
contour.linewidth = 1
contour.linecolor = 'red'

panel = MapPanel()
panel.area = 'us'
panel.projection = 'lcc'
panel.layers = ['coastline']
panel.left_title = '700-hPa Temperature'
panel.right_title = 'Valid at a time'
panel.title = 'Plot of data'
panel.plots = [contour]

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

return pc.figure


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

0 comments on commit eb0efa1

Please sign in to comment.