Created
June 26, 2018 16:18
-
-
Save khaeru/ff18b8b3bc2c10910a6ccf2406922f40 to your computer and use it in GitHub Desktop.
Like partial(), for plotnine geoms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def geom_partial(name, aes, **geom_kwargs): | |
"""Like partial(), for plotnine geoms. | |
*name* refers to a geom, e.g. 'point' for p9.geom_point. *geom_kwargs* are | |
optional keyword arguments for the geom. *aes* is a dict() with keys that | |
match the geom's aesthetics, and values that are format strings. | |
Returns a callable object. When called, the arguments are passed to | |
str.format() for each entry in *aes*, and a plotnine geom is returned with | |
the resulting aesthetics and kwargs. | |
""" | |
def f(*args, extra={}, **kwargs): | |
geom = getattr(p9, f'geom_{name}') | |
aes_kwargs = {k: v.format(*args, **kwargs) for k, v in aes.items()} | |
geom_kwargs.update(extra) | |
return geom(mapping=p9.aes(**aes_kwargs), **geom_kwargs) | |
return f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment