Skip to content

Commit 4a722cc

Browse files
authored
Merge pull request #20 from ksunden/patch_rect
Some simple rectangle patches
2 parents 2cd647f + 7fad912 commit 4a722cc

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

data_prototype/patches.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import numpy as np
2+
3+
from .wrappers import ProxyWrapper, _stale_wrapper
4+
from .containers import DataContainer
5+
6+
from matplotlib.patches import Patch as _Patch, Rectangle as _Rectangle
7+
8+
9+
class PatchWrapper(ProxyWrapper):
10+
_wrapped_class = _Patch
11+
_privtized_methods = (
12+
"get_edgecolor",
13+
"get_facecolor",
14+
"get_linewidth",
15+
"get_linestyle",
16+
"get_antialiased",
17+
"get_hatch",
18+
"get_fill",
19+
"get_capstyle",
20+
"get_joinstyle",
21+
"get_path",
22+
"set_edgecolor",
23+
"set_facecolor",
24+
"set_linewidth",
25+
"set_linestyle",
26+
"set_antialiased",
27+
"set_hatch",
28+
"set_fill",
29+
"set_capstyle",
30+
"set_joinstyle",
31+
"set_path",
32+
)
33+
_xunits = ()
34+
_yunits = ()
35+
required_keys = {
36+
"edgecolor",
37+
"facecolor",
38+
"linewidth",
39+
"linestyle",
40+
"antialiased",
41+
"hatch",
42+
"fill",
43+
"capstyle",
44+
"joinstyle",
45+
}
46+
47+
def __init__(self, data: DataContainer, nus=None, /, **kwargs):
48+
super().__init__(data, nus)
49+
self._wrapped_instance = self._wrapped_class([0, 0], 0, 0, **kwargs)
50+
51+
@_stale_wrapper
52+
def draw(self, renderer):
53+
self._update_wrapped(self._query_and_transform(renderer, xunits=self._xunits, yunits=self._yunits))
54+
return self._wrapped_instance.draw(renderer)
55+
56+
def _update_wrapped(self, data):
57+
for k, v in data.items():
58+
getattr(self._wrapped_instance, f"set_{k}")(v)
59+
60+
61+
class RectangleWrapper(PatchWrapper):
62+
_wrapped_class = _Rectangle
63+
_privtized_methods = PatchWrapper._privtized_methods + (
64+
"get_x",
65+
"get_y",
66+
"get_width",
67+
"get_height",
68+
"get_angle",
69+
"get_rotation_point" "set_x",
70+
"set_y",
71+
"set_width",
72+
"set_height",
73+
"set_angle",
74+
"set_rotation_point",
75+
)
76+
_xunits = ("x", "width")
77+
_yunits = ("y", "height")
78+
required_keys = PatchWrapper.required_keys | {"x", "y", "width", "height", "angle", "rotation_point"}
79+
80+
def _update_wrapped(self, data):
81+
for k, v in data.items():
82+
if k == "rotation_point":
83+
self._wrapped_instance.rotation_point = v
84+
continue
85+
# linestyle and hatch do not work as arrays,
86+
# but ArrayContainer requires arrays, so index into an array if needed
87+
elif k in ("linestyle", "hatch"):
88+
if isinstance(v, np.ndarray):
89+
v = v[0]
90+
getattr(self._wrapped_instance, f"set_{k}")(v)

examples/simple_patch.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
====================
3+
Simple patch artists
4+
====================
5+
6+
"""
7+
import numpy as np
8+
9+
import matplotlib.pyplot as plt
10+
11+
from data_prototype.containers import ArrayContainer
12+
13+
from data_prototype.patches import RectangleWrapper
14+
15+
cont1 = ArrayContainer(
16+
x=np.array([-3]),
17+
y=np.array([0]),
18+
width=np.array([2]),
19+
height=np.array([3]),
20+
angle=np.array([0]),
21+
rotation_point=np.array(["center"]),
22+
edgecolor=np.array([0, 0, 0]),
23+
facecolor=np.array([0.0, 0.7, 0, 0.5]),
24+
linewidth=np.array([3]),
25+
linestyle=np.array(["-"]),
26+
antialiased=np.array([True]),
27+
hatch=np.array(["*"]),
28+
fill=np.array([True]),
29+
capstyle=np.array(["round"]),
30+
joinstyle=np.array(["miter"]),
31+
)
32+
33+
cont2 = ArrayContainer(
34+
x=np.array([0]),
35+
y=np.array([1]),
36+
width=np.array([2]),
37+
height=np.array([3]),
38+
angle=np.array([30]),
39+
rotation_point=np.array(["center"]),
40+
edgecolor=np.array([0, 0, 0]),
41+
facecolor=np.array([0.7, 0, 0]),
42+
linewidth=np.array([6]),
43+
linestyle=np.array(["-"]),
44+
antialiased=np.array([True]),
45+
hatch=np.array([""]),
46+
fill=np.array([True]),
47+
capstyle=np.array(["butt"]),
48+
joinstyle=np.array(["round"]),
49+
)
50+
51+
fig, ax = plt.subplots()
52+
ax.set_xlim(-5, 5)
53+
ax.set_ylim(0, 5)
54+
rect1 = RectangleWrapper(cont1, {})
55+
rect2 = RectangleWrapper(cont2, {})
56+
ax.add_artist(rect1)
57+
ax.add_artist(rect2)
58+
ax.set_aspect(1)
59+
plt.show()

0 commit comments

Comments
 (0)