dppd_plotnine¶
Welcome to dppd_plotnine, which converts plotnine to work with dppd and pythonifies it’s api.
It’s source lives at github.
Quickstart¶
import numpy as np
from dppd import dppd
import dppd_plotnine
from plotnine.data import mtcars
import plotnine as p9
dp, X = dppd()
plot = (
dp(mtcars)
.assign(kwh=X.hp * 0.74)
.categorize("cyl")
.p9()
.add_point(
"cyl",
"kwh",
color="cyl",
position=p9.position_jitter(height=0, random_state=500),
)
.add_errorbar(
x="cyl",
y="kwh_median",
ymin="kwh_median",
ymax="kwh_median",
data=dp(X.data)
.groupby("cyl")
.summarize(("kwh", np.median, "kwh_median"))
.pd,
)
.scale_color_manual(
["red", "blue", "purple"]
) # after pd, X is what it was before
.pd
)
plot.save("test.png")

dppd_plotnine supports two different call conventions, one matching plotnine (and ggplot) and another, perhaps more convinient one, see call convention.
Contents¶
Changes from plotnine¶
Call convention¶
The dppd_plotnine interface differes in two aspects from plotnine (and R’s ggplot2) API:
Method-chaining replaces addition
#plotnine
p = p9.ggplot(df)
p += p9.geom_point(p9.aes('x','y'), color='red')
#dppd_plotnine
dp(df).p9.geom_point(p9.aes('x','y'), color='red').pd
And optionally, aes mapping and kwargs are replaced by kwargs starting with and without underscore:
dp(df).p9.add_point('x', y='y', _color='red').pd
All geom_* functions can be called as add_* with the following changes to the calling api:
- all REQUIRED_AES can be passed by position. The order is x,y, then alphabetically
- kwargs that name an aes are mapped (ie. as if they were passed to mapping=p9.aes(…)).
- kwargs starting with a ‘_’ are treated as unmapped (ie. _color= get’s passed as color= kwarg to the geom)
- data, position and stat are left alone.
- data=None turns the geom into an annotation.
This pythonifies the interface a bit and get’s rid of the p9.aes boilerplate, while maintaining full expressiveness.
Note that this also allows you to use geom_hline(300) without having to type y_intercept=…
Ways to pass in data¶
- As an expression evaluated in the context of the DataFrame, using patsy, mapped:
add_point(x='df_column * 5', ...)
. Note that column named ‘x * 5’ takes precedence over a column ‘x’ multiplied by 5. - As a mapped list, the size of the DataFrame:
add_point(x=['a','b','c], ...)
- As an unmapped list, the size of the DataFrame:
add_point(_color=['red','blue','red','blue'])
- As a mapped scalar:
add_point(x='"a"' ...)
, note the inner quoting! - As a unmapped scalar:
add_point(..., _color='red')
- A mapping may refer to stat derived variables, add_bar(x=’x’, y=’stat(count)’, stat=p9.stat_count)
- If data=None is passed, the geom is treated as an annotation and a DataFrame is
constructed from the values of the mappend and unmapped arguments:
add_point(data=None, x=5, y=10, _color='red')
Other changes from plotnine¶
- the default stat on geom_bar is stat_identity.
- save is verbose=False by default and returns the plot object
- save is aliased to render
- add_scatter is an alias for add_point
- there is a small set of convinence wrappers - see
dppd_plotnine.plotnine_extensions
License¶
The MIT License (MIT)
Copyright (c) 2018 Florian Finkernagel
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Contributors¶
- Florian Finkernagel <finkernagel@imt.uni-marburg.de>
Changelog¶
Version 0.2.4¶
- annotation_stripes -> annotation_stripes_dppd (we still have some improvements from the plotnine default)
- experimental .cyberpunk() aesthetics
- scale_x/y_continious aliases (sxc, sxc10 for log10, sxc2 for log())
- .hide_legend
- .figure_size/fig_size/size for adjusting size in jupyter notebooks
- .add_cummulative
Version 0.2.1¶
- version no longer managed by git tags
- tests no longer need ‘mbf_qualitycontrol’
Version 0.2¶
- fixed packag ename
- plots now understand ‘render_args’ which are passed to save later on.
- turn axis labels
- improved tests
Version 0.1¶
- Feature A added
- FIX: nasty bug #1729 fixed
- add your changes here!