{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Altair: Statistical Visualization for Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Altair](http://github.com/altair-viz/altair/) provides a declarative Python API for statistical visualization, built on top of [Vega-Lite](http://vega.github.io/vega-lite/).\n", "\n", "The notebooks listed here provide a set of tutorials and examples of Altair's use. For more complete documentation, see [Altair's Documentation](http://altair-viz.github.io/).\n", "\n", "Please note that these notebooks assume Altair version 2.0 or later:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'4.0.0'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import altair\n", "altair.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## About Altair\n", "\n", "Altair is built on [Vega-Lite](http://vega.github.io/vega-lite/). From the Vega-lite documentation:\n", "\n", "> Vega-Lite specifications describe visualizations as mappings from data to properties of graphical marks (e.g., points or bars). It automatically produces visualization components including axes, legends, and scales. It then determines properties of these components based on a set of carefully designed rules. This approach allows Vega-Lite specifications to be succinct and expressive, but also provide user control. As Vega-Lite is designed for analysis, it supports data transformations such as aggregation, binning, filtering, sorting, and visual transformations including stacking and faceting.\n", "\n", "The key feature of this declarative approach is that the user is free to think about the data, rather than the mechanics of the visualization. Vega-Lite specifications are expressed in JavaScript Object Notation (JSON), a cross-platform format often used for storage of nested and/or hierarchical data. Altair builds a Python layer on top of this, so that rather than writing raw JSON strings the user can write declarative Python code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Quick Altair example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is a quick example of the Altair API in action:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import altair as alt\n", "from vega_datasets import data\n", "\n", "cars = data.cars()\n", "\n", "chart = alt.Chart(cars).mark_circle().encode(\n", " x='Horsepower',\n", " y='Miles_per_Gallon',\n", " color='Origin',\n", ")\n", "\n", "chart" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In most cases, the data passed to the ``Chart`` will be a standard Pandas dataframe, as we can see here:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " | Name | \n", "Miles_per_Gallon | \n", "Cylinders | \n", "Displacement | \n", "Horsepower | \n", "Weight_in_lbs | \n", "Acceleration | \n", "Year | \n", "Origin | \n", "
---|---|---|---|---|---|---|---|---|---|
0 | \n", "chevrolet chevelle malibu | \n", "18.0 | \n", "8 | \n", "307.0 | \n", "130.0 | \n", "3504 | \n", "12.0 | \n", "1970-01-01 | \n", "USA | \n", "
1 | \n", "buick skylark 320 | \n", "15.0 | \n", "8 | \n", "350.0 | \n", "165.0 | \n", "3693 | \n", "11.5 | \n", "1970-01-01 | \n", "USA | \n", "
2 | \n", "plymouth satellite | \n", "18.0 | \n", "8 | \n", "318.0 | \n", "150.0 | \n", "3436 | \n", "11.0 | \n", "1970-01-01 | \n", "USA | \n", "
3 | \n", "amc rebel sst | \n", "16.0 | \n", "8 | \n", "304.0 | \n", "150.0 | \n", "3433 | \n", "12.0 | \n", "1970-01-01 | \n", "USA | \n", "
4 | \n", "ford torino | \n", "17.0 | \n", "8 | \n", "302.0 | \n", "140.0 | \n", "3449 | \n", "10.5 | \n", "1970-01-01 | \n", "USA | \n", "