Skip to content

POC: Add a LegendSpec class for building legend specifications - #4870

Draft
seisman wants to merge 1 commit into
mainfrom
legend/spec-class
Draft

POC: Add a LegendSpec class for building legend specifications#4870
seisman wants to merge 1 commit into
mainfrom
legend/spec-class

Conversation

@seisman

@seisman seisman commented Aug 28, 2026

Copy link
Copy Markdown
Member

Implement a LegendSpec class to create legend specification file in a more readable way (initial idea from #1513 (comment)).

This is still POC. So only three legend records are implemented (xref: https://docs.generic-mapping-tools.org/6.7/legend.html#legend-codes).

import pygmt
from pygmt.params import LegendSpec

# Build a legend specification 
spec = LegendSpec()
spec.add_header("My Legend")
spec.add_symbol(symbol="c", size=0.2, label="Circle", pen="1p,black")
spec.add_symbol(symbol="s", size=0.3, label="Square", fill="blue", pen="2p,red", dx1=0.5, dx2=2.0)
spec.add_line(length=1.0, pen="1p,blue", label="Line 1")
spec.add_line(length=1.0, pen="1p,red,-", label="Line 2", dx1=0.5, dx2=2.0)

print(spec)

fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True)
fig.legend(spec=spec)
fig.show()

The output is:

H - My Legend
S - c 0.2 - 1p,black - Circle
S 0.5 s 0.3 blue 2p,red 2.0 Square
S - - 1.0 - 1p,blue - Line 1
S 0.5 - 1.0 - 1p,red,- 2.0 Line 2
legend

@seisman
seisman force-pushed the legend/spec-class branch 2 times, most recently from 3abf2eb to bf14e42 Compare August 28, 2026 16:46
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@seisman
seisman force-pushed the legend/spec-class branch from bf14e42 to 93f1888 Compare August 28, 2026 17:48
@seisman seisman added feature Brand new feature discussions Need more discussion before taking further actions labels Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

discussions Need more discussion before taking further actions feature Brand new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant