Skip to content

Commit c493b3d

Browse files
iangowhas2k1
authored andcommitted
Document custom themeable extension path
1 parent 2b3e1cc commit c493b3d

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

plotnine/themes/theme.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class theme:
8585
8686
These simply bind together all the aspects of a themeable
8787
that can be themed. See [](`~plotnine.themes.themeable.themeable`).
88+
Extension packages may provide additional themeables by defining
89+
and importing subclasses of `themeable`, then passing values for
90+
them as keyword arguments to `theme`.
8891
8992
Notes
9093
-----

plotnine/themes/themeable.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ class axis_title(axis_title_x, axis_title_y):
8989
9090
Notes
9191
-----
92-
A user should never create instances of class
93-
[](`~plotnine.themes.themeable.Themeable`) or subclasses of it.
92+
Most users should not create instances of class
93+
[](`~plotnine.themes.themeable.themeable`) or subclasses of it
94+
directly. Extension authors may define subclasses; they are registered
95+
by class name when their module is imported and can be used through
96+
[](`~plotnine.themes.theme.theme`) keyword arguments.
9497
"""
9598

9699
def __init__(self, theme_element: element_base | str | float):

tests/test_theme.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
theme_xkcd,
3636
)
3737
from plotnine.data import mtcars
38-
from plotnine.themes.themeable import panel_border
38+
from plotnine.coords.coord_cartesian import coord_cartesian
39+
from plotnine.themes.themeable import panel_border, themeable
3940

4041
LT_MPL310 = version.parse(mpl.__version__) < version.parse("3.10")
4142
IS_CI = bool(os.environ.get("CI"))
@@ -124,6 +125,48 @@ def test_blank_panel_border_hides_polar_spine():
124125
plt.close(fig)
125126

126127

128+
def test_extension_themeable_applies_from_theme_kwargs():
129+
class test_extension_panel_facecolor(themeable):
130+
def apply_ax(self, ax):
131+
super().apply_ax(ax)
132+
ax.set_facecolor(self.properties["value"])
133+
134+
p = (
135+
ggplot(mtcars, aes(x="wt", y="mpg"))
136+
+ geom_point()
137+
+ theme(test_extension_panel_facecolor="red")
138+
)
139+
140+
fig = p.draw()
141+
try:
142+
assert fig.axes[0].get_facecolor() == (1.0, 0.0, 0.0, 1.0)
143+
finally:
144+
plt.close(fig)
145+
146+
147+
def test_coord_can_read_extension_themeable():
148+
class test_extension_coord_title(themeable):
149+
pass
150+
151+
class coord_reads_themeable(coord_cartesian):
152+
def setup_ax(self, ax, panel_params, theme):
153+
super().setup_ax(ax, panel_params, theme)
154+
ax.set_title(theme.getp("test_extension_coord_title"))
155+
156+
p = (
157+
ggplot(mtcars, aes(x="wt", y="mpg"))
158+
+ geom_point()
159+
+ coord_reads_themeable()
160+
+ theme(test_extension_coord_title="coord themeable")
161+
)
162+
163+
fig = p.draw()
164+
try:
165+
assert fig.axes[0].get_title() == "coord themeable"
166+
finally:
167+
plt.close(fig)
168+
169+
127170
def test_element_line_dashed_capstyle():
128171
p = ggplot(mtcars, aes(x="wt", y="mpg")) + theme(
129172
panel_grid=element_line(

0 commit comments

Comments
 (0)