Altair¶
Altair is a declarative statistical visualisation library for Python based on Vega and Vega-Lite.
See also:
Installation¶
$ pipenv install altair
Altair has the following dependencies, which are automatically installed with the above installation command:
Example¶
Here is an example of using the Altair API to quickly visualise a data set with an interactive scatter plot:
Import
[1]:
import altair as alt
Loading a simple data set as a pandas DataFrame
[2]:
from vega_datasets import data
cars = data.cars()
[3]:
cars.head()
[3]:
Name | Miles_per_Gallon | Cylinders | Displacement | Horsepower | Weight_in_lbs | Acceleration | Year | Origin | |
---|---|---|---|---|---|---|---|---|---|
0 | chevrolet chevelle malibu | 18.0 | 8 | 307.0 | 130.0 | 3504 | 12.0 | 1970-01-01 | USA |
1 | buick skylark 320 | 15.0 | 8 | 350.0 | 165.0 | 3693 | 11.5 | 1970-01-01 | USA |
2 | plymouth satellite | 18.0 | 8 | 318.0 | 150.0 | 3436 | 11.0 | 1970-01-01 | USA |
3 | amc rebel sst | 16.0 | 8 | 304.0 | 150.0 | 3433 | 12.0 | 1970-01-01 | USA |
4 | ford torino | 17.0 | 8 | 302.0 | 140.0 | 3449 | 10.5 | 1970-01-01 | USA |
Creating an interactive scatter diagram
[4]:
alt.Chart(cars).mark_point().encode(
x="Horsepower",
y="Miles_per_Gallon",
color="Origin",
).interactive()
[4]:
The main idea is that you can declare links between data columns and visual dimensions such as the x-axis, y-axis, colour, etc. The rest of the details of the display are handled automatically. The remaining details of the visualisation are handled automatically. Building on this declarative plot idea, a relatively concise grammar can be used to create an amazing range of plots and visualisations, from simple to sophisticated.