diff --git a/pygmt/base_plotting.py b/pygmt/base_plotting.py index f54cf2e2bd1..64dfbbbbf5d 100644 --- a/pygmt/base_plotting.py +++ b/pygmt/base_plotting.py @@ -621,7 +621,7 @@ def image(self, imagefile, **kwargs): @fmt_docstring @use_alias(R="region", J="projection", D="position", F="box") @kwargs_to_strings(R="sequence") - def legend(self, spec=None, **kwargs): + def legend(self, spec=None, position="JTR+jTR+o0.2c", box="+gwhite+p1p", **kwargs): """ Plot legends on maps. @@ -644,13 +644,23 @@ def legend(self, spec=None, **kwargs): {R} position (D) : str ``'[g|j|J|n|x]refpoint+wwidth[/height][+jjustify][+lspacing][+odx[/dy]]'`` - Defines the reference point on the map for the legend. + Defines the reference point on the map for the legend. By default, uses + 'JTR+jTR+o0.2c' which places the legend at the top-right corner inside + the map frame, with a 0.2 cm offset. box (F) : bool or str ``'[+cclearances][+gfill][+i[[gap/]pen]][+p[pen]][+r[radius]][+s[[dx/dy/][shade]]]'`` Without further options, draws a rectangular border around the - legend using **MAP_FRAME_PEN**. + legend using **MAP_FRAME_PEN**. By default, uses '+gwhite+p1p' which draws + a box around the legend using a 1 point black pen and adds a white background. """ kwargs = self._preprocess(**kwargs) + + if "D" not in kwargs: + kwargs["D"] = position + + if "F" not in kwargs: + kwargs["F"] = box + with Session() as lib: if spec is None: specfile = "" diff --git a/pygmt/tests/baseline/test_legend_default_position.png b/pygmt/tests/baseline/test_legend_default_position.png new file mode 100644 index 00000000000..67bd7f2adc2 Binary files /dev/null and b/pygmt/tests/baseline/test_legend_default_position.png differ diff --git a/pygmt/tests/test_legend.py b/pygmt/tests/test_legend.py index b966b691e32..525574c5f90 100644 --- a/pygmt/tests/test_legend.py +++ b/pygmt/tests/test_legend.py @@ -28,6 +28,22 @@ def test_legend_position(): return fig +@pytest.mark.mpl_image_compare +def test_legend_default_position(): + """ + Try using the default legend position. + """ + + fig = Figure() + + fig.basemap(region=[-1, 1, -1, 1], frame=True) + + fig.plot(x=[0], y=[0], style="p10p", label="Default") + fig.legend() + + return fig + + @pytest.mark.mpl_image_compare def test_legend_entries(): """