{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Line Charts\n", "\n", "* [Simple line chart](#Simple-line-chart)\n", " * [Data transform in Vega-Lite](#Data-transform-in-Vega-Lite)\n", " * [Data transform in Pandas](#Data-transform-in-Pandas)\n", " * [Log scale](#Log-scale)\n", "* [Grouped line chart](#Grouped-line-chart)\n", "* [Line chart with custom path](#Line-chart-with-custom-path)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import altair as alt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from vega_datasets import data\n", "stocks = data.stocks()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simple line chart" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Data transform in Vega-Lite" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alt.Chart(stocks).mark_line().encode(\n", " x='date:T',\n", " y='price:Q'\n", ").transform_filter(\"datum.symbol == 'GOOG'\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Data transform in Pandas" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "GOOG = stocks.query('symbol == \"GOOG\"')\n", "\n", "alt.Chart(GOOG).mark_line().encode(\n", " x='date:T',\n", " y='price:Q'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Log scale" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alt.Chart(GOOG).mark_line().encode(\n", " y=alt.Y('price:Q', scale=alt.Scale(type='log')),\n", " x='date:T',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Grouped line chart" ] }, { "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": [ "alt.Chart(stocks).mark_line().encode(\n", " x='date:T',\n", " y='price',\n", " color='symbol',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Line chart with custom path" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "driving = data.driving()\n", "\n", "alt.Chart(driving).mark_line().encode(\n", " alt.X('miles', scale=alt.Scale(zero=False)),\n", " alt.Y('gas', scale=alt.Scale(zero=False)),\n", " order='year:T',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Layer points and lines with custom path" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base = alt.Chart(driving).encode(\n", " alt.X('miles', scale=alt.Scale(zero=False)),\n", " alt.Y('gas', scale=alt.Scale(zero=False)),\n", " order='year:T',\n", ")\n", "\n", "base.mark_line() + base.mark_point()" ] } ], "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 }