plotnine is an implementation of a grammar of graphics in Python based on ggplot2. The grammar allows you to compose plots by explicitly mapping variables in a dataframe to the visual characteristics (position, color, size etc.) of objects that make up the plot.
Plotting with a grammar of graphics is powerful. Custom (and otherwise complex) plots are easy to think about and build incrementally, while the simple plots remain simple to create.
To learn more about how to use plotnine, check out the documentation. Since plotnine has an API similar to ggplot2, where it lacks in coverage the ggplot2 documentation may be helpful.
from plotnine import *
from plotnine.data import mtcars
Building a complex plot piece by piece.
-
Scatter plot
( ggplot(mtcars, aes("wt", "mpg")) + geom_point() )
-
Scatter plot colored according some variable
( ggplot(mtcars, aes("wt", "mpg", color="factor(gear)")) + geom_point() )
-
Scatter plot colored according some variable and smoothed with a linear model with confidence intervals.
( ggplot(mtcars, aes("wt", "mpg", color="factor(gear)")) + geom_point() + stat_smooth(method="lm") )
-
Scatter plot colored according some variable, smoothed with a linear model with confidence intervals and plotted on separate panels.
( ggplot(mtcars, aes("wt", "mpg", color="factor(gear)")) + geom_point() + stat_smooth(method="lm") + facet_wrap("gear") )
-
Adjust the themes
I) Make it playful
( ggplot(mtcars, aes("wt", "mpg", color="factor(gear)")) + geom_point() + stat_smooth(method="lm") + facet_wrap("gear") + theme_xkcd() )
II) Or professional
( ggplot(mtcars, aes("wt", "mpg", color="factor(gear)")) + geom_point() + stat_smooth(method="lm") + facet_wrap("gear") + theme_tufte() )
Official release
# Using pip
$ pip install plotnine # 1. should be sufficient for most
$ pip install 'plotnine[extra]' # 2. includes extra/optional packages
$ pip install 'plotnine[test]' # 3. testing
$ pip install 'plotnine[doc]' # 4. generating docs
$ pip install 'plotnine[dev]' # 5. development (making releases)
$ pip install 'plotnine[all]' # 6. everything
# Or using conda
$ conda install -c conda-forge plotnine
Development version
$ pip install git+https://github.com/has2k1/plotnine.git
Our documentation could use some examples, but we are looking for something a little bit special. We have two criteria:
- Simple looking plots that otherwise require a trick or two.
- Plots that are part of a data analytic narrative. That is, they provide
some form of clarity showing off the
geom
,stat
, ... at their differential best.
If you come up with something that meets those criteria, we would love to see it. See plotnine-examples.
If you discover a bug checkout the issues if it has not been reported, yet please file an issue.
And if you can fix a bug, your contribution is welcome.
Plotnine has tests that generate images which are compared to baseline images known
to be correct. To generate images that are consistent across all systems you have
to install matplotlib from source. You can do that with pip
using the command.
$ pip install matplotlib --no-binary matplotlib
Otherwise there may be small differences in the text rendering that throw off the image comparisons.