Skip to content

Commit 5816270

Browse files
committed
Add the Pattern class
1 parent 926a690 commit 5816270

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

examples/tutorials/advanced/cartesian_histograms.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# Import the required packages
1919
import numpy as np
2020
import pygmt
21+
from pygmt.params import Pattern
2122

2223
# %%
2324
# Generate random data from a normal distribution:
@@ -204,10 +205,8 @@
204205
frame=["wSnE", "xaf10", "ya5f1+lCumulative counts"],
205206
data=data01,
206207
series=10,
207-
# Use pattern ("p") number 8 as fill for the bars
208-
# Set the background ("+b") to white [Default]
209-
# Set the foreground ("+f") to black [Default]
210-
fill="p8+bwhite+fblack",
208+
# Fill bars with GMT pattern 8, with white background and black foreground.
209+
fill=Pattern(8, bgcolor="white", fgcolor="black"),
211210
pen="1p,darkgray,solid",
212211
histtype=0,
213212
# Show cumulative counts

examples/tutorials/advanced/focal_mechanisms.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# %%
1919
import pandas as pd
2020
import pygmt
21+
from pygmt.params import Pattern
2122

2223
# Set up arguments for basemap
2324
region = [-5, 5, -5, 5]
@@ -122,8 +123,8 @@
122123
longitude=2,
123124
latitude=0,
124125
depth=0,
125-
compressionfill="p8",
126-
extensionfill="p31",
126+
compressionfill=Pattern(8),
127+
extensionfill=Pattern(31),
127128
outline=True,
128129
)
129130

pygmt/params/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
"""
44

55
from pygmt.params.box import Box
6+
from pygmt.params.pattern import Pattern

pygmt/params/pattern.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
The Pattern class for specifying GMT filling patterns.
3+
"""
4+
5+
import dataclasses
6+
7+
from pygmt.alias import Alias
8+
from pygmt.exceptions import GMTValueError
9+
from pygmt.params.base import BaseParam
10+
11+
12+
@dataclasses.dataclass(repr=False)
13+
class Pattern(BaseParam):
14+
"""
15+
16+
Examples
17+
--------
18+
>>> from pygmt.params import Pattern
19+
>>> str(Pattern(id=1))
20+
'p1'
21+
>>> str(Pattern(id=1, bgcolor="red", fgcolor="blue"))
22+
'p1+bred+fblue'
23+
>>> str(Pattern(id=1, bgcolor="red", fgcolor="blue", dpi=300))
24+
'p1+bred+fblue+r300'
25+
>>> str(Pattern(id="my_pattern.png"))
26+
'pmy_pattern.png'
27+
>>> str(Pattern(id=2, reversed=True))
28+
'P2'
29+
>>> str(Pattern(id=100))
30+
Traceback (most recent call last):
31+
...
32+
pygmt.exceptions.GMTValueError: Invalid pattern id: 100...
33+
"""
34+
35+
id: int | str
36+
reversed: bool = False
37+
bgcolor: str | None = None
38+
fgcolor: str | None = None
39+
dpi: int | None = None
40+
41+
def __post_init__(self):
42+
"""
43+
Validate the id and set the reversed flag.
44+
"""
45+
if isinstance(self.id, int) and not (1 <= self.id <= 90):
46+
raise GMTValueError(
47+
self.id,
48+
description="pattern id",
49+
reason=(
50+
"Pattern id must be an integer in the range 1-90 "
51+
"or the name of a 1-, 8-, or 24-bit image raster file"
52+
),
53+
)
54+
55+
@property
56+
def _aliases(self):
57+
"""
58+
Aliases for the Pattern class.
59+
"""
60+
return [
61+
Alias(self.id, name="id", prefix="P" if self.reversed else "p"),
62+
Alias(self.bgcolor, name="bgcolor", prefix="+b"),
63+
Alias(self.fgcolor, name="fgcolor", prefix="+f"),
64+
Alias(self.dpi, name="dpi", prefix="+r"),
65+
]

0 commit comments

Comments
 (0)