{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Making Better Bar Charts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Chapter 3, we learned how to make simple bar charts as well as stacked bar charts. This recipe is all about how to make these charts more impactful and aesthetically better. We will start with a simple stacked bar chart and iteratively improve it " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting Ready" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this recipe, we are going to create a `DataFrame` containing the Year-on-year changes in electricity demand by region from 2019 to 2025. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "dta = pd.DataFrame(data=[\n", "[269.1, 716.9, 211.8, 455.0, 432.2, 503.1],\n", "[-120.6, 118.1, 114.5, 91.4, 88.6, 81.7],\n", "[-3.5, 51.3, 59.6, 42.4, 68.3, 74.8] ,\n", "[-77.3, 101.6, 109.5, -25.9, 50.5, 57.3],\n", "[-105.8, 125.6, -94.9, 39.5, 37.0, 40],\n", "[-151.4, 307.5, 97.6, 102.6, 182.5, 181.5]],\n", "index = [ 'China', 'India', 'Southeast Asia', 'United States', 'European Union', 'Others'],\n", "columns = [2020, 2021, 2022, 2023, 2024, 2025]\n", ").T.rename_axis('Year')\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "dta['Total'] = dta.sum(axis=1).values" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | China | \n", "India | \n", "Southeast Asia | \n", "United States | \n", "European Union | \n", "Others | \n", "Total | \n", "
---|---|---|---|---|---|---|---|
Year | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
2020 | \n", "269.1 | \n", "-120.6 | \n", "-3.5 | \n", "-77.3 | \n", "-105.8 | \n", "-151.4 | \n", "-189.5 | \n", "
2021 | \n", "716.9 | \n", "118.1 | \n", "51.3 | \n", "101.6 | \n", "125.6 | \n", "307.5 | \n", "1421.0 | \n", "
2022 | \n", "211.8 | \n", "114.5 | \n", "59.6 | \n", "109.5 | \n", "-94.9 | \n", "97.6 | \n", "498.1 | \n", "
2023 | \n", "455.0 | \n", "91.4 | \n", "42.4 | \n", "-25.9 | \n", "39.5 | \n", "102.6 | \n", "705.0 | \n", "
2024 | \n", "432.2 | \n", "88.6 | \n", "68.3 | \n", "50.5 | \n", "37.0 | \n", "182.5 | \n", "859.1 | \n", "
2025 | \n", "503.1 | \n", "81.7 | \n", "74.8 | \n", "57.3 | \n", "40.0 | \n", "181.5 | \n", "938.4 | \n", "