Skip to content

Commit b5fe0d4

Browse files
authored
Figure.text: Allow passing a sequence of offset to the 'offset' parameter (#4652)
1 parent 5a11bbe commit b5fe0d4

9 files changed

Lines changed: 25 additions & 31 deletions

File tree

examples/gallery/images/cross_section.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
x=[lonA, lonB],
6262
y=[latA, latB],
6363
text=["A", "B"],
64-
offset="0c/0.3c", # Move text 0.2 centimeters up (y-direction)
64+
offset=(0, 0.3), # Move text 0.3 centimeters up (y-direction)
6565
font="15p,red", # Use a red font with a size of 15 points
6666
)
6767

examples/gallery/lines/decorated_lines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
text=decoline,
8181
font="Courier-Bold",
8282
justify="ML",
83-
offset="0.75c/0c",
83+
offset=(0.75, 0),
8484
)
8585

8686
fig.show()

examples/gallery/lines/linefronts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
text=frontstyle,
9191
font="Courier-Bold",
9292
justify="ML",
93-
offset="0.75c/0c",
93+
offset=(0.75, 0),
9494
)
9595

9696
fig.show()

examples/gallery/lines/linestyles.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
# Plot the line using the default line style
3535
fig.plot(x=x, y=y)
36-
fig.text(x=x[-1], y=y[-1], text="solid (default)", justify="ML", offset="0.2c/0c")
36+
fig.text(x=x[-1], y=y[-1], text="solid (default)", justify="ML", offset=(0.2, 0))
3737

3838
# Plot the line using different line styles
3939
for linestyle in [
@@ -48,14 +48,14 @@
4848
]:
4949
y -= 1 # Move the current line down
5050
fig.plot(x=x, y=y, pen=linestyle)
51-
fig.text(x=x[-1], y=y[-1], text=linestyle, justify="ML", offset="0.2c/0c")
51+
fig.text(x=x[-1], y=y[-1], text=linestyle, justify="ML", offset=(0.2, 0))
5252

5353
# Plot the line like a railway track (black/white).
5454
# The trick here is plotting the same line twice but with different line styles
5555
y -= 1 # move the current line down
5656
fig.plot(x=x, y=y, pen="5p,black")
5757
fig.plot(x=x, y=y, pen="4p,white,20p_20p")
58-
fig.text(x=x[-1], y=y[-1], text="5p,black", justify="ML", offset="0.2c/0.2c")
59-
fig.text(x=x[-1], y=y[-1], text="4p,white,20p_20p", justify="ML", offset="0.2c/-0.2c")
58+
fig.text(x=x[-1], y=y[-1], text="5p,black", justify="ML", offset=(0.2, 0.2))
59+
fig.text(x=x[-1], y=y[-1], text="4p,white,20p_20p", justify="ML", offset=(0.2, -0.2))
6060

6161
fig.show()

examples/gallery/lines/quoted_lines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
text=quotedline,
7171
font="Courier-Bold",
7272
justify="ML",
73-
offset="0.75c/0c",
73+
offset=(0.75, 0),
7474
)
7575

7676
fig.show()

examples/gallery/lines/vector_heads_tails.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
x=x, y=y, style=vecstyle, direction=([angle], [length]), pen="2p", fill="red3"
8383
)
8484
fig.text(
85-
x=6, y=y, text=vecstyle, font="Courier-Bold", justify="ML", offset="0.2c/0c"
85+
x=6, y=y, text=vecstyle, font="Courier-Bold", justify="ML", offset=(0.2, 0)
8686
)
8787
y -= 1 # move the next vector down
8888

examples/gallery/symbols/points_cities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
x=cities_large.geometry.x,
4242
y=cities_large.geometry.y,
4343
text=cities_large["name"],
44-
offset="0.12c",
44+
offset=0.12,
4545
justify="BL",
4646
font="6p,Helvetica-Bold",
4747
fill="white@30",

examples/tutorials/basics/text.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
# Shift the text label relatively to the position given via the x and y parameters
6060
# by 1 centimeter to the right (positive x-direction) and 0.5 centimeters down
6161
# (negative y-direction)
62-
fig.text(x=0, y=-3, text="my text", offset="1c/-0.5c")
62+
fig.text(x=0, y=-3, text="my text", offset=(1, -0.5))
6363

6464
fig.shift_origin(xshift="w+0.5c")
6565

@@ -196,19 +196,13 @@
196196
fig = pygmt.Figure()
197197

198198
# -----------------------------------------------------------------------------
199-
# Left: Add a tag to a subplot
199+
# Left: Add a tag to a subplot at the Top Left corner
200200
fig.basemap(
201201
region=[-5, 5, -5, 5],
202202
projection="X5c",
203203
frame=Frame(axes="WStr", axis=Axis(annot=True, tick=True)),
204204
)
205-
206-
fig.text(
207-
text="(a)",
208-
position="TL", # Top Left
209-
justify="TL", # Top Left
210-
offset="0.1c/-0.1c",
211-
)
205+
fig.text(text="(a)", position="TL", justify="TL", offset=(0.1, -0.1))
212206

213207
fig.shift_origin(xshift="w+1c")
214208

@@ -224,7 +218,7 @@
224218
text="@@100 km", # "@@" gives "@" in GMT or PyGMT
225219
position="TC", # Top Center
226220
justify="MC", # Middle Center
227-
offset="0c/0.2c",
221+
offset=(0, 0.2),
228222
no_clip=True, # Allow plotting outside of the plot frame
229223
)
230224

pygmt/src/text.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
@fmt_docstring
2626
@use_alias(
2727
C="clearance",
28-
D="offset",
2928
a="aspatial",
3029
e="find",
3130
f="coltypes",
@@ -45,6 +44,7 @@ def text( # noqa: PLR0912, PLR0913, PLR0915
4544
fill: str | None = None,
4645
pen: str | None = None,
4746
justify: bool | None | AnchorCode | Sequence[AnchorCode] = None,
47+
offset: Sequence[float | str] | str | None = None,
4848
no_clip: bool = False,
4949
projection: str | None = None,
5050
region: Sequence[float | str] | str | None = None,
@@ -77,6 +77,7 @@ def text( # noqa: PLR0912, PLR0913, PLR0915
7777
7878
$aliases
7979
- B = frame
80+
- D = offset
8081
- F = **+a**: angle, **+c**: position, **+j**: justify, **+f**: font
8182
- G = fill
8283
- J = projection
@@ -157,16 +158,14 @@ def text( # noqa: PLR0912, PLR0913, PLR0915
157158
pen
158159
Set the pen used to draw a rectangle around the text string (see ``clearance``)
159160
[Default is ``"0.25p,black,solid"``].
160-
offset : str
161-
[**j**\|\ **J**]\ *dx*\[/*dy*][**+v**\[*pen*]].
162-
Offset the text from the projected (x, y) point by *dx*/\ *dy*
163-
[Default is ``"0/0"``].
164-
If *dy* is not specified then it is set equal to *dx*. Use **j** to
165-
offset the text away from the point instead (i.e., the text
166-
justification will determine the direction of the shift). Using
167-
**J** will shorten diagonal offsets at corners by sqrt(2).
168-
Optionally, append **+v** which will draw a line from the original
169-
point to the shifted point; append a pen to change the attributes
161+
offset
162+
(*dx*, *dy*) or [**j**\|\ **J**]\ *dx*\[/*dy*][**+v**\[*pen*]].
163+
Offset the text from the projected (x, y) point by (*dx*, *dy*) [Default is
164+
(0, 0)]. If *dy* is not specified then it is set equal to *dx*. Use **j** to
165+
offset the text away from the point instead (i.e., the text justification will
166+
determine the direction of the shift). Using **J** will shorten diagonal offsets
167+
at corners by sqrt(2). Optionally, append **+v** which will draw a line from
168+
the original point to the shifted point; append a pen to change the attributes
170169
for this line.
171170
no_clip
172171
Do **not** clip text at the frame boundaries [Default is ``False``].
@@ -281,6 +280,7 @@ def text( # noqa: PLR0912, PLR0913, PLR0915
281280
)
282281

283282
aliasdict = AliasSystem(
283+
D=Alias(offset, name="offset", sep="/", size=2),
284284
G=Alias(fill, name="fill"),
285285
N=Alias(no_clip, name="no_clip"),
286286
W=Alias(pen, name="pen"),

0 commit comments

Comments
 (0)