-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathelement_text.py
More file actions
218 lines (201 loc) · 6.39 KB
/
Copy pathelement_text.py
File metadata and controls
218 lines (201 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
Theme elements used to decorate the graph.
"""
from __future__ import annotations
from contextlib import suppress
from typing import TYPE_CHECKING
from .element_base import element_base
from .margin import margin as Margin
if TYPE_CHECKING:
from typing import Any, Literal, Sequence
from plotnine import theme
class element_text(element_base):
"""
Theme element: Text
Parameters
----------
family :
Font family. See [](`~matplotlib.text.Text.set_family`)
for supported values.
style :
Font style
color :
Text color
weight :
Should be one of `normal`, `bold`, `heavy`, `light`,
`ultrabold` or `ultralight`.
size :
text size
ha :
Horizontal Alignment.
va :
Vertical alignment.
ma :
Horizontal Alignment for multiline text.
rotation :
Rotation angle in the range [0, 360]. The `rotation` is affected
by the `rotation_mode`.
rotation_mode :
How to do the rotation. If `None` or `"default"`, first rotate
the text then align the bounding box of the rotated text.
If `"anchor"`, first align the unrotated text then rotate the
text around the point of alignment.
linespacing : float
Line spacing
backgroundcolor :
Background color
margin :
Margin around the text. The keys are
`t`, `b`, `l`, `r` and `units`.
The `tblr` keys are floats.
The `unit` is one of `pt`, `lines` or `in`.
Not all text themeables support margin parameters and other
than the `units`, only some of the other keys may apply.
kwargs :
Parameters recognised by [](`~matplotlib.text.Text`)
Notes
-----
[](`~plotnine.themes.element_text`) will accept parameters that
conform to the **ggplot2** *element_text* API, but it is preferable
the **Matplotlib** based API described above.
"""
def __init__(
self,
family: str | list[str] | None = None,
style: str | Sequence[str] | None = None,
weight: int | str | Sequence[int | str] | None = None,
color: (
str
| tuple[float, float, float]
| tuple[float, float, float, float]
| Sequence[
str
| tuple[float, float, float]
| tuple[float, float, float, float]
]
| None
) = None,
size: float | Sequence[float] | None = None,
ha: (
Literal["center", "left", "right"]
| float
| Sequence[Literal["center", "left", "right"] | float]
| None
) = None,
va: (
Literal["center", "top", "bottom", "baseline", "center_baseline"]
| float
| Sequence[
Literal[
"center", "top", "bottom", "baseline", "center_baseline"
]
| float
]
| None
) = None,
ma: Literal["center", "left", "right"] | float | None = None,
rotation: (
Literal["vertical", "horizontal"]
| float
| Sequence[Literal["vertical", "horizontal"]]
| Sequence[float]
| None
) = None,
linespacing: float | None = None,
backgroundcolor: (
str
| tuple[float, float, float]
| tuple[float, float, float, float]
| Sequence[
str
| tuple[float, float, float]
| tuple[float, float, float, float]
]
| None
) = None,
margin: (
Margin | dict[Literal["t", "b", "l", "r", "unit"], Any] | None
) = None,
rotation_mode: Literal["default", "anchor"] | None = None,
**kwargs: Any,
):
# ggplot2 translation
with suppress(KeyError):
linespacing = kwargs.pop("lineheight")
with suppress(KeyError):
color = color or kwargs.pop("colour")
with suppress(KeyError):
_face = kwargs.pop("face")
if _face == "plain":
style = "normal"
elif _face == "italic":
style = "italic"
elif _face == "bold":
weight = "bold"
elif _face == "bold.italic":
style = "italic"
weight = "bold"
with suppress(KeyError):
ha = self._translate_hjust(kwargs.pop("hjust"))
with suppress(KeyError):
va = self._translate_vjust(kwargs.pop("vjust"))
with suppress(KeyError):
rotation = kwargs.pop("angle")
super().__init__()
self.properties.update(**kwargs)
if margin is not None:
if isinstance(margin, dict):
if "units" in margin:
# for backward compatibility
margin["unit"] = margin.pop("units") # pyright: ignore[reportArgumentType]
margin = Margin(**margin)
self.properties["margin"] = margin
# Use the parameters that have been set
names = (
"backgroundcolor",
"color",
"family",
"ha",
"linespacing",
"rotation",
"size",
"style",
"va",
"ma",
"weight",
"rotation_mode",
)
variables = locals()
for name in names:
if variables[name] is not None:
self.properties[name] = variables[name]
def setup(self, theme: theme, themeable_name: str):
"""
Setup the theme_element before drawing
"""
if m := self.properties.get("margin"):
m.setup(theme, themeable_name)
def _translate_hjust(
self, just: float
) -> Literal["left", "right", "center"]:
"""
Translate ggplot2 justification from [0, 1] to left, right, center.
"""
if just == 0:
return "left"
elif just == 1:
return "right"
else:
return "center"
def _translate_vjust(
self, just: float
) -> Literal["top", "bottom", "center"]:
"""
Translate ggplot2 justification from [0, 1] to top, bottom, center.
"""
if just == 0:
return "bottom"
elif just == 1:
return "top"
else:
return "center"