{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "parameters" ] }, "outputs": [], "source": [ "flex_title = \"Altair plots - Scroll Layout\"\n", "flex_vertical_layout = \"scroll\"\n", "flex_author = \"built using jupyter-flex\"\n", "flex_source_link = \"https://github.com/danielfrg/jupyter-flex/blob/master/examples/plots/altair-scroll.ipynb\"\n", "flex_include_source = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Page 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Row 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple Scatter Plot with Tooltips" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import altair as alt\n", "from vega_datasets import data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "alt.renderers.set_embed_options(actions=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "source" ] }, "outputs": [], "source": [ "np.random.seed(42)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "source = data.cars()\n", "\n", "plot = alt.Chart(source).mark_circle(size=60).encode(\n", " x='Horsepower',\n", " y='Miles_per_Gallon',\n", " color='Origin',\n", " tooltip=['Name', 'Origin', 'Horsepower', 'Miles_per_Gallon']\n", ")\n", "plot.interactive()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple bar chart" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "source = pd.DataFrame({\n", " 'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],\n", " 'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]\n", "})\n", "\n", "plot = alt.Chart(source).mark_bar().encode(\n", " x='a',\n", " y='b'\n", ")\n", "plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Row 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bar Chart with Negative Values" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "source = data.us_employment()\n", "\n", "alt.Chart(source).mark_bar().encode(\n", " x=\"month:T\",\n", " y=\"nonfarm_change:Q\",\n", " color=alt.condition(\n", " alt.datum.nonfarm_change > 0,\n", " alt.value(\"steelblue\"), # The positive color\n", " alt.value(\"orange\") # The negative color\n", " )\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple Heatmap" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "# Compute x^2 + y^2 across a 2D grid\n", "x, y = np.meshgrid(range(-5, 5), range(-5, 5))\n", "z = x ** 2 + y ** 2\n", "\n", "# Convert this grid to columnar data expected by Altair\n", "source = pd.DataFrame({'x': x.ravel(),\n", " 'y': y.ravel(),\n", " 'z': z.ravel()})\n", "\n", "plot = alt.Chart(source).mark_rect().encode(\n", " x='x:O',\n", " y='y:O',\n", " color='z:Q'\n", ")\n", "plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Stacked Bar Chart" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "source = data.barley()\n", "\n", "alt.Chart(source).mark_bar().encode(\n", " x='variety',\n", " y='sum(yield)',\n", " color='site'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Row 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Filled Step Chart" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "source = data.stocks()\n", "\n", "alt.Chart(source).mark_area(\n", " color=\"lightblue\",\n", " interpolate='step-after',\n", " line=True\n", ").encode(\n", " x='date',\n", " y='price'\n", ").transform_filter(alt.datum.symbol == 'GOOG').properties(\n", " width='container'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Page 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Row 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multi Series Line Chart" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "source = data.stocks()\n", "\n", "alt.Chart(source).mark_line().encode(\n", " x='date',\n", " y='price',\n", " color='symbol'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Cumulative Count Chart" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "source = data.movies.url\n", "\n", "alt.Chart(source).transform_window(\n", " cumulative_count=\"count()\",\n", " sort=[{\"field\": \"IMDB_Rating\"}],\n", ").mark_area().encode(\n", " x=\"IMDB_Rating:Q\",\n", " y=\"cumulative_count:Q\"\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Row 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Stacked Density Estimates" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "source = data.iris()\n", "\n", "alt.Chart(source).transform_fold(\n", " ['petalWidth',\n", " 'petalLength',\n", " 'sepalWidth',\n", " 'sepalLength'],\n", " as_ = ['Measurement_type', 'value']\n", ").transform_density(\n", " density='value',\n", " bandwidth=0.3,\n", " groupby=['Measurement_type'],\n", " extent= [0, 8],\n", " counts = True,\n", " steps=200\n", ").mark_area().encode(\n", " alt.X('value:Q'),\n", " alt.Y('density:Q', stack='zero'),\n", " alt.Color('Measurement_type:N')\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Binned Scatterplot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "source = data.movies.url\n", "\n", "alt.Chart(source).mark_circle().encode(\n", " alt.X('IMDB_Rating:Q', bin=True),\n", " alt.Y('Rotten_Tomatoes_Rating:Q', bin=True),\n", " size='count()'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Row 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multifeature Scatter Plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "source = data.iris()\n", "\n", "alt.Chart(source).mark_circle().encode(\n", " alt.X('sepalLength', scale=alt.Scale(zero=False)),\n", " alt.Y('sepalWidth', scale=alt.Scale(zero=False, padding=1)),\n", " color='species',\n", " size='petalWidth'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Choropleth Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "body" ] }, "outputs": [], "source": [ "from vega_datasets import data\n", "\n", "counties = alt.topo_feature(data.us_10m.url, 'counties')\n", "source = data.unemployment.url\n", "\n", "alt.Chart(counties).mark_geoshape().encode(\n", " color='rate:Q'\n", ").transform_lookup(\n", " lookup='id',\n", " from_=alt.LookupData(source, 'id', ['rate'])\n", ").project(\n", " type='albersUsa'\n", ").properties(\n", " width=\"container\",\n", " height=\"container\"\n", ")" ] } ], "metadata": { "celltoolbar": "Tags", "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 }