{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Altair Debugging Guide\n", "\n", "In this notebook we show you common debugging techniques that you can use if you run into issues with Altair. \n", "\n", "You can jump to the following sections:\n", "\n", "* [Installation and Setup](#Installation) when Altair is not installed correctly\n", "* [Display Issues](#Display-Troubleshooting) when you don't see a chart\n", "* [Invalid Specifications](#Invalid-Specifications) when you get an error\n", "* [Properties are Being Ignored](#Properties-are-Being-Ignored) when you don't see any errors or warnings\n", "* [Asking for Help](#Asking-for-Help) when you get stuck\n", "* [Reporting Issues](#Reporting-Issues) when you find a bug\n", "\n", "In addition to this notebook, you might find the [Frequently Asked Questions](https://altair-viz.github.io/user_guide/faq.html) and [Display Troubleshooting](https://altair-viz.github.io/user_guide/troubleshooting.html) guides helpful. \n", "\n", "_This notebook is part of the [data visualization curriculum](https://github.com/uwdata/visualization-curriculum)._" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Installation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These instructions follow [the Altair documentation](https://altair-viz.github.io/getting_started/installation.html) but focus on some specifics for this series of notebooks. \n", "\n", "In every notebook, we will import the [Altair](https://github.com/altair-viz/altair) and [Vega Datasets](https://github.com/altair-viz/vega_datasets) packages. If you are running this notebook on [Colab](https://colab.research.google.com), Altair and Vega Datasets should be preinstalled and ready to go. The notebooks in this series are designed for Colab but should also work in Jupyter Lab or the Jupyter Notebook (the notebook requires a bit more setup [described below](#Special-Setup-for-the-Jupyter-Notebook)) but additional packages are required. \n", "\n", "If you are running in Jupyter Lab or Jupyter Notebooks, you have to install the necessary packages by running the following command in your terminal.\n", "\n", "```bash\n", "pip install altair vega_datasets\n", "```\n", "\n", "Or if you use [Conda](https://conda.io)\n", "\n", "```bash\n", "conda install -c conda-forge altair vega_datasets\n", "```\n", "\n", "You can run command line commands from a code cell by prefixing it with `!`. For example, to install Altair and Vega Datasets with [Pip](https://pip.pypa.io/), you can run the following cell." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: altair in /anaconda3/lib/python3.7/site-packages (4.0.1)\n", "Requirement already satisfied: vega_datasets in /anaconda3/lib/python3.7/site-packages (0.7.0)\n", "Requirement already satisfied: numpy in /anaconda3/lib/python3.7/site-packages (from altair) (1.15.4)\n", "Requirement already satisfied: jsonschema in /anaconda3/lib/python3.7/site-packages (from altair) (2.6.0)\n", "Requirement already satisfied: pandas in /anaconda3/lib/python3.7/site-packages (from altair) (0.23.4)\n", "Requirement already satisfied: toolz in /anaconda3/lib/python3.7/site-packages (from altair) (0.9.0)\n", "Requirement already satisfied: jinja2 in /anaconda3/lib/python3.7/site-packages (from altair) (2.10)\n", "Requirement already satisfied: entrypoints in /anaconda3/lib/python3.7/site-packages (from altair) (0.2.3)\n", "Requirement already satisfied: python-dateutil>=2.5.0 in /anaconda3/lib/python3.7/site-packages (from pandas->altair) (2.7.5)\n", "Requirement already satisfied: pytz>=2011k in /anaconda3/lib/python3.7/site-packages (from pandas->altair) (2018.7)\n", "Requirement already satisfied: MarkupSafe>=0.23 in /anaconda3/lib/python3.7/site-packages (from jinja2->altair) (1.1.0)\n", "Requirement already satisfied: six>=1.5 in /anaconda3/lib/python3.7/site-packages (from python-dateutil>=2.5.0->pandas->altair) (1.12.0)\n" ] } ], "source": [ "!pip install altair vega_datasets" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import altair as alt\n", "from vega_datasets import data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Make sure you are Using the Latest Version of Altair" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you are running into issues with Altair, first make sure that you are running the latest version. To check the version of Altair that you have installed, run the cell below. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'4.0.1'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alt.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To check what the latest version of altair is, go to [this page](https://pypi.org/project/altair/) or run the cell below (requires Python 3)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.0.1\n" ] } ], "source": [ "import urllib.request, json \n", "with urllib.request.urlopen(\"https://pypi.org/pypi/altair/json\") as url:\n", " print(json.loads(url.read().decode())['info']['version'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you are not running the latest version, you can update it with `pip`. You can update Altair and Vega Datasets by running this command in your terminal.\n", "\n", "```\n", "pip install -U altair vega_datasets\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Try Making a Chart" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now you can create an Altair chart." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iris = data.iris()\n", "\n", "alt.Chart(iris).mark_point().encode(\n", " x='petalLength',\n", " y='petalWidth',\n", " color='species'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Special Setup for the Jupyter Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you are running in Jupyter Lab, Jupyter Notebook, or Colab (and have a working Internet connection) you should be seeing a chart. If you are running in another environment (or offline), you will need to tell Altair to use a different renderer;\n", "\n", "To activate a different renderer in a notebook cell:\n", "\n", "```python\n", "# to run in nteract, VSCode, or offline in JupyterLab\n", "alt.renderers.enable('mimebundle')\n", "\n", "```\n", "\n", "To run offline in Jupyter Notebook you must install an additional dependency, the `vega` package. Run this command in your terminal:\n", "\n", "```bash\n", "pip install vega\n", "```\n", "\n", "Then activate the notebook renderer:\n", "\n", "```python\n", "# to run offline in Jupyter Notebook\n", "alt.renderers.enable('notebook')\n", "\n", "```\n", "\n", "\n", "These instruction follow [the instructions on the Altair website](https://altair-viz.github.io/getting_started/installation.html#installation-notebook)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display Troubleshooting\n", "\n", "If you are having issues with seeing a chart, make sure your setup is correct by following the [debugging instruction above](#installation). If you are still having issues, follow the [instruction about debugging display issues in the Altair documentation](https://altair-viz.github.io/user_guide/troubleshooting.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Non Existent Fields\n", "\n", "A common error is [accidentally using a field that does not exit](https://altair-viz.github.io/user_guide/troubleshooting.html#plot-displays-but-the-content-is-empty). " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "df = pd.DataFrame({'x': [1, 2, 3],\n", " 'y': [3, 1, 4]})\n", "\n", "alt.Chart(df).mark_point().encode(\n", " x='x:Q',\n", " y='y:Q',\n", " color='color:Q' # <-- this field does not exist in the data!\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check the spelling of your files and print the data source to confirm that the data and fields exist. For instance, here you see that `color` is not a vaid field. " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " | x | \n", "y | \n", "
---|---|---|
0 | \n", "1 | \n", "3 | \n", "
1 | \n", "2 | \n", "1 | \n", "
2 | \n", "3 | \n", "4 | \n", "