Plotting with Altair¶
Plotting Libraries in Python¶
The linchpin library for plotting in Python is without a doubt
matplotlib. While infinitely flexible, however, for many applications
matplotlib lacks user-friendly tools for quickly making common types of figures (scatter plots, linear fits, histograms, etc.). With that in mind, several other packages (most of which are actually built on
matplotlib) have been created to provide a more user-friendly interface. Unlike in
matplotlib, where you have to think in terms of the geometric objects you want to place on axes, all three of these alternative libraries allow for higher-level, more “declarative” code:
plotninealso has a clear philosophy that underlies its syntax, and seems to be relatively popular among pure python users.
plotnineis designed to replicate the syntax of the extremely popular R package
ggplotin Python. In most cases it works wonderfully. As a result, if you are already familiar with
plotnineis hard to beat.
seabornis also a declarative plotting language, although its syntax is a little less modular than
altair– e.g. there is a distinct command for plotting histograms, a command for kernel densities, a command for bivariate fits, etc. But still much easier than
Why Do We Plot Data?¶
Grammer of Graphics¶
Transformations (skip for now?)
Building a Scatterplot¶
Saving a Plot¶
What’s Going on Beneath the Hood?¶
If you are enrolled in Practical Data Science at Duke, don’t do these exercises on your own – we’ll do them in class!