{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import hvplot.pandas # noqa" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction\n", "\n", "A `bar` plot represents **categorical data** with rectangular bars with heights proportional to the **numerical values** that they represent.\n", "The x-axis represents the categories and the y axis represents the numerical value scale.\n", "The bars are of equal width which allows for instant comparison of data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pd.DataFrame({\n", " \"framework\": [\"hvPlot\", \"HoloViews\", \"Panel\"], \n", " \"stars\": [700, 2400, 2600]\n", "}).hvplot.bar(x=\"framework\", y=\"stars\", color=\"gold\", title=\"Bar Plot of Github Stars\", ylabel=\"⭐\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data\n", "\n", "Let's import some data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from bokeh.sampledata.autompg import autompg_clean as autompg\n", "\n", "autompg.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We define *long form data*, i.e. one row per `yr` categorical value." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "autompg_long_form = autompg.groupby(\"yr\").mean(numeric_only=True).reset_index()\n", "autompg_long_form.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " We define a dataset with a *multi index* representing multiple categories" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "autompg_multi_index = autompg.query(\"yr<=80\").groupby(['yr', 'origin']).mean(numeric_only=True)\n", "autompg_multi_index.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We define *wide form data*, i.e. multiple columns representing a category like `origin`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "autompg_wide = autompg_multi_index.reset_index().pivot(index='yr', columns='origin', values='mpg')\n", "autompg_wide.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Bar Plots\n", "\n", "You can plot **long form data** if you specify the categorical x-value using the `x` argument and the numerical y-value using the `y-argument`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "autompg_long_form.hvplot.bar(x=\"yr\", y=\"mpg\", width=1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you don't specify the `x` argument, then the index will be used." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "autompg_long_form.hvplot.bar(y=\"mpg\", width=1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When the index is a `MultiIndex`, the x-axis represents the multiple categories included in the index, the outer index level being displayed as the outer category." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "autompg_multi_index.hvplot.bar(width=1000, rot=90)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can instead stack on the y-axis the values of the nested index/category, *origin* in this example, by setting `stacked` to `True`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "autompg_multi_index.hvplot.bar(stacked=True, width=1000, legend=\"top_left\", height=500)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To plot multiple categories on the x-axis when the data is **wide form**, you need to provide a list of columns to `y`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "autompg_wide.hvplot.bar(y=['Asia', 'Europe', 'North America'], width=1000, ylabel=\"mpg\", rot=90)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And you may also stack the values of the *wide form* data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "autompg_wide.hvplot.bar(y=['Asia', 'Europe', 'North America'], ylabel=\"mpg\", stacked=True, width=1000, legend=\"top_left\", height=500)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Colorful Bar Plots\n", "\n", "You can control the `bar` color using the `color` argument. It accepts the name of a column, the name of a color or a list of colors.\n", "\n", "Here is an example using a single named color." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "autompg_long_form.hvplot.bar(x=\"yr\", y=\"mpg\", color=\"teal\", width=1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is an example using a list of colors." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "autompg_wide.hvplot.bar(y=['Asia', 'Europe', 'North America'], width=1000, ylabel=\"mpg\", color=[\"#ba2649\", \"#ffa7ca\", \"#1a6b54\"], rot=90)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is an example using the name of a column." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "autompg_long_form.hvplot.bar(y='mpg', color=\"weight\", colorbar=True, clabel=\"Weight\", cmap=\"bmy\", width=1000)" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 4 }