{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Bar Chart Examples" ] }, { "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", "population = data.population()\n", "\n", "y2k_pop = population.query('year == 2000')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Single bar chart" ] }, { "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(y2k_pop).mark_bar().encode(\n", " x='sum(people)',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bar chart" ] }, { "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": [ "alt.Chart(y2k_pop).mark_bar().encode(\n", " x='sum(people)',\n", " y='age:O'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pandas Series to sorted bar chart" ] }, { "cell_type": "code", "execution_count": 5, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countstitle
a9.0a
b186.0b
c49.0c
d42.0d
e16.0e
f48.0f
g58.0g
h28.0h
\n", "
" ], "text/plain": [ " counts title\n", "a 9.0 a\n", "b 186.0 b\n", "c 49.0 c\n", "d 42.0 d\n", "e 16.0 e\n", "f 48.0 f\n", "g 58.0 g\n", "h 28.0 h" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "data = pd.DataFrame()\n", "data['counts'] = pd.Series(np.round(100 * np.abs(np.random.randn(8))))\n", "data.index = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']\n", "data['title'] = data.index\n", "data" ] }, { "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(data).mark_bar().encode(\n", " x=alt.X('counts', axis=alt.Axis(title='Counts')),\n", " y=alt.Y('title',\n", " sort=alt.EncodingSortField(field='counts', order='ascending', op='sum'),\n", " axis=alt.Axis(title='Title')\n", " ),\n", " color=alt.Color('title:N')\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stacked bar chart" ] }, { "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": [ "chart = alt.Chart(y2k_pop).mark_bar().encode(\n", " x='age:O',\n", " y='sum(people):Q',\n", " color=alt.Color('sex:N', scale=alt.Scale(range=[\"#e377c2\",\"#1f77b4\"]))\n", ")\n", "chart" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "chart = alt.Chart(population).transform_filter(\n", " \"datum.year == 2000\"\n", ").transform_calculate(\n", " \"sex\", \"datum.sex == 1 ? 'Male' : 'Female'\"\n", ").encode(\n", " color=alt.Color('sex:N', scale=alt.Scale(range=[\"#e377c2\",\"#1f77b4\"]))\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chart.mark_bar().encode(\n", " x='age:O',\n", " y='sum(people):Q',\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Layered bar chart" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Re-using the transform from above:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chart.mark_bar().encode(\n", " x='age:O',\n", " y=alt.Y('sum(people):Q', stack=None),\n", " opacity=alt.value(0.7)\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Normalized stacked bar chart" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chart.mark_bar().encode(\n", " x='age:O',\n", " y=alt.Y('sum(people):Q', stack='normalize'),\n", " opacity=alt.value(0.7)\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Grouped bar chart" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chart.mark_bar().encode(\n", " alt.X('sex:N', axis=alt.Axis(title=None, labels=False)),\n", " alt.Y('sum(people):Q'),\n", " alt.Column('age:O')\n", ").properties(\n", " width=20 # width of one column facet\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Histogram" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from vega_datasets import data\n", "movies = data.movies()\n", "\n", "alt.Chart(movies).mark_bar().encode(\n", " x=alt.X('IMDB_Rating', bin=alt.Bin(maxbins=10)),\n", " y='count(*):Q',\n", ")" ] } ], "metadata": { "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 }