Skip to content

Commit 49a7263

Browse files
liamtoneyweiji14
authored andcommitted
Implement default position/box for legend (#359)
Checks for presence of position/D arg before assigning default position ("JTR+jTR+o0.2c") and box ("+gwhite+p1p").
1 parent 59dbcbe commit 49a7263

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

pygmt/base_plotting.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ def image(self, imagefile, **kwargs):
621621
@fmt_docstring
622622
@use_alias(R="region", J="projection", D="position", F="box")
623623
@kwargs_to_strings(R="sequence")
624-
def legend(self, spec=None, **kwargs):
624+
def legend(self, spec=None, position="JTR+jTR+o0.2c", box="+gwhite+p1p", **kwargs):
625625
"""
626626
Plot legends on maps.
627627
@@ -644,13 +644,23 @@ def legend(self, spec=None, **kwargs):
644644
{R}
645645
position (D) : str
646646
``'[g|j|J|n|x]refpoint+wwidth[/height][+jjustify][+lspacing][+odx[/dy]]'``
647-
Defines the reference point on the map for the legend.
647+
Defines the reference point on the map for the legend. By default, uses
648+
'JTR+jTR+o0.2c' which places the legend at the top-right corner inside
649+
the map frame, with a 0.2 cm offset.
648650
box (F) : bool or str
649651
``'[+cclearances][+gfill][+i[[gap/]pen]][+p[pen]][+r[radius]][+s[[dx/dy/][shade]]]'``
650652
Without further options, draws a rectangular border around the
651-
legend using **MAP_FRAME_PEN**.
653+
legend using **MAP_FRAME_PEN**. By default, uses '+gwhite+p1p' which draws
654+
a box around the legend using a 1 point black pen and adds a white background.
652655
"""
653656
kwargs = self._preprocess(**kwargs)
657+
658+
if "D" not in kwargs:
659+
kwargs["D"] = position
660+
661+
if "F" not in kwargs:
662+
kwargs["F"] = box
663+
654664
with Session() as lib:
655665
if spec is None:
656666
specfile = ""
Loading

pygmt/tests/test_legend.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ def test_legend_position():
2828
return fig
2929

3030

31+
@pytest.mark.mpl_image_compare
32+
def test_legend_default_position():
33+
"""
34+
Try using the default legend position.
35+
"""
36+
37+
fig = Figure()
38+
39+
fig.basemap(region=[-1, 1, -1, 1], frame=True)
40+
41+
fig.plot(x=[0], y=[0], style="p10p", label="Default")
42+
fig.legend()
43+
44+
return fig
45+
46+
3147
@pytest.mark.mpl_image_compare
3248
def test_legend_entries():
3349
"""

0 commit comments

Comments
 (0)