Code
pip install altairAltair is a declarative statistical visualization library for python.
For more details on the library read here
To install the package use the following code:
You only need to install a package once but you need to reload it every time you start a new session
When specifying data in Altair we use pandas Data Frame objects.
Use of pandas Data Frame will prompt altair to store entire data set in JSON format.
The syntax for altair is equivalent to the ggplot function in R.
The ggplot() function one provides the following:
The altair syntax is given as follows:
An encoding is a visual property of the objects in your plot and they include things like:
We demonstrate how to use the altair library we load the faithful data set from this repository
| eruptions | waiting | |
|---|---|---|
| 0 | 3.600 | 79 |
| 1 | 1.800 | 54 |
| 2 | 3.333 | 74 |
| 3 | 2.283 | 62 |
| 4 | 4.533 | 85 |
A basic scatter plot is given in Figure 1:
Figure 1 shows a positive and a linear relationship between eruptions and waiting time of the geysers.
We demonstrate the usability of aesthetics in altair library using the mpg dataset.
We graph displ versus hwy in the mpg dataset and then color by the class of the car see Figure 2.
One can configure the encoding properties of your mark manually as shown in the code below:
A way to add additional variables is with facets. This works especially for categorical variables.
One can do a scatter plot with a super-imposed line of fit.
Note: A mark is a geometrical object that a plot uses to represent data.
A sample of line graphis given in
An alternative to the altair library in Python is the plotnine package in Python
We briefly give an illustration of the plotnine library. For more details read this documentation
plotnine is an implementation of a grammar of graphics in Python that is based on ggplot2.
The grammar allows you to compose plots by explicitly mapping variables in a dataframe to the visual objects that make up the plot.
To install plotnine run the following code:
The code to do the basic plot is: