{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Layered Charts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A `LayeredChart` allows you to stack multiple individual charts on top of each other as layers. For example, this could be used to create a chart with both lines and points." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import altair as alt\n", "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "np.random.seed(181)\n", "data = pd.DataFrame({'x': np.arange(10),\n", " 'y':np.random.rand(10)})" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
xy
000.524023
110.114090
220.570567
330.405836
440.182134
\n", "
" ], "text/plain": [ " x y\n", "0 0 0.524023\n", "1 1 0.114090\n", "2 2 0.570567\n", "3 3 0.405836\n", "4 4 0.182134" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Layered charts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose you have defined two charts, and you would like to plot them on the same set of axes.\n", "This comes up often when creating a compound chart with points and lines marking the same data.\n", "For example:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "layer1 = alt.Chart(data).mark_point().encode(\n", " x='x:Q',\n", " y='y:Q'\n", ")\n", "\n", "layer2 = alt.Chart(data).mark_line().encode(\n", " x='x:Q',\n", " y='y:Q'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The most succinct way to layer two charts is to use the ``+`` operator:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "layer1 + layer2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One problem with this is that you end up specifying the encodings and data multiple times; you can instead build both layers from the same base chart:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base = alt.Chart(data).encode(\n", " x='x:Q',\n", " y='y:Q'\n", ")\n", "\n", "base.mark_line() + base.mark_point()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To be a bit more explicit, you can use the ``alt.layer`` function, which produces the same thing:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alt.layer(\n", " base.mark_line(),\n", " base.mark_point()\n", ")" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }