Skip to content

Instantly share code, notes, and snippets.

@khaeru
Created June 26, 2018 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khaeru/ff18b8b3bc2c10910a6ccf2406922f40 to your computer and use it in GitHub Desktop.
Save khaeru/ff18b8b3bc2c10910a6ccf2406922f40 to your computer and use it in GitHub Desktop.
Like partial(), for plotnine geoms
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