{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating Heat maps" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Heat map charts are a versatile data visualization technique that uses color to represent values in a two-dimensional matrix or grid. Each cell in the heat map corresponds to a combination of two variables, and the color intensity or hue indicates the magnitude of the data point. This approach makes it easy to identify patterns, trends, and outliers across large datasets. Heat maps are particularly useful when working with data that has a natural tabular structure, such as correlation matrices, sales performance by product and region, or time-series data by categories.\n", "\n", "One powerful application of heat maps is in illustrating yield curves’ term structures over time. In this context, the x-axis could represent time (e.g., months or years), while the y-axis shows the maturity periods of bonds (e.g., 3 months, 2 years, 10 years). The color in each cell would reflect the yield for a given maturity at a specific time, allowing analysts to quickly observe changes in interest rate environments and identify trends like steepening or flattening yield curves. This makes heat maps an invaluable tool for financial professionals seeking insights into interest rate dynamics.\n", "\n", "Despite their utility, heat maps should be avoided in cases where precise numerical values are critical, as the reliance on color gradation can obscure exact measurements. They are also less effective when the dataset is small or lacks meaningful variability, as the visualization may appear sparse or uninformative. Additionally, choosing an inappropriate color scale or failing to consider colorblind accessibility can lead to misinterpretation. Heat maps work best when the goal is to identify high-level patterns and trends across large datasets, rather than focusing on detailed or highly specific comparisons." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting ready" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this recipe we will use data from the Federal Reserve Economic Data which is saved in a spreadsheet" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Import the `pandas` Python library as `pd`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Use the function `read_csv` to read the data. Make sure you set the argument `parse_dates` as `True` to guarantee that the dates are being parsed correctly" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "data = pd.read_csv('data/data_fed_yc.csv', parse_dates=True).set_index('DATE')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Inspect the data by calling the method `head` on the `DataFrame`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | 30-year | \n", "10-year | \n", "5-year | \n", "3-year | \n", "2-year | \n", "1-year | \n", "6-month | \n", "3-month | \n", "1-month | \n", "
---|---|---|---|---|---|---|---|---|---|
DATE | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
1990-01-31 | \n", "8.26 | \n", "8.21 | \n", "8.12 | \n", "8.13 | \n", "8.09 | \n", "7.92 | \n", "7.96 | \n", "7.90 | \n", "NaN | \n", "
1990-02-28 | \n", "8.50 | \n", "8.47 | \n", "8.42 | \n", "8.39 | \n", "8.37 | \n", "8.11 | \n", "8.12 | \n", "8.00 | \n", "NaN | \n", "
1990-03-31 | \n", "8.56 | \n", "8.59 | \n", "8.60 | \n", "8.63 | \n", "8.63 | \n", "8.35 | \n", "8.28 | \n", "8.17 | \n", "NaN | \n", "
1990-04-30 | \n", "8.76 | \n", "8.79 | \n", "8.77 | \n", "8.78 | \n", "8.72 | \n", "8.40 | \n", "8.27 | \n", "8.04 | \n", "NaN | \n", "
1990-05-31 | \n", "8.73 | \n", "8.76 | \n", "8.74 | \n", "8.69 | \n", "8.64 | \n", "8.32 | \n", "8.19 | \n", "8.01 | \n", "NaN | \n", "
DATE | \n", "1990-01-31 | \n", "1990-02-28 | \n", "1990-03-31 | \n", "1990-04-30 | \n", "1990-05-31 | \n", "1990-06-30 | \n", "1990-07-31 | \n", "1990-08-31 | \n", "1990-09-30 | \n", "1990-10-31 | \n", "... | \n", "2023-07-31 | \n", "2023-08-31 | \n", "2023-09-30 | \n", "2023-10-31 | \n", "2023-11-30 | \n", "2023-12-31 | \n", "2024-01-31 | \n", "2024-02-29 | \n", "2024-03-31 | \n", "2024-04-30 | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1-month | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "... | \n", "5.39 | \n", "5.54 | \n", "5.53 | \n", "5.57 | \n", "5.53 | \n", "5.54 | \n", "5.54 | \n", "5.49 | \n", "5.51 | \n", "5.48 | \n", "
3-month | \n", "7.90 | \n", "8.00 | \n", "8.17 | \n", "8.04 | \n", "8.01 | \n", "7.99 | \n", "7.87 | \n", "7.69 | \n", "7.60 | \n", "7.40 | \n", "... | \n", "5.49 | \n", "5.56 | \n", "5.56 | \n", "5.60 | \n", "5.52 | \n", "5.44 | \n", "5.45 | \n", "5.44 | \n", "5.47 | \n", "5.44 | \n", "
6-month | \n", "7.96 | \n", "8.12 | \n", "8.28 | \n", "8.27 | \n", "8.19 | \n", "8.05 | \n", "7.92 | \n", "7.77 | \n", "7.70 | \n", "7.53 | \n", "... | \n", "5.53 | \n", "5.54 | \n", "5.51 | \n", "5.57 | \n", "5.44 | \n", "5.34 | \n", "5.21 | \n", "5.28 | \n", "5.36 | \n", "5.38 | \n", "
1-year | \n", "7.92 | \n", "8.11 | \n", "8.35 | \n", "8.40 | \n", "8.32 | \n", "8.10 | \n", "7.94 | \n", "7.78 | \n", "7.76 | \n", "7.55 | \n", "... | \n", "5.37 | \n", "5.37 | \n", "5.44 | \n", "5.42 | \n", "5.28 | \n", "4.96 | \n", "4.79 | \n", "4.92 | \n", "4.99 | \n", "5.14 | \n", "
2-year | \n", "8.09 | \n", "8.37 | \n", "8.63 | \n", "8.72 | \n", "8.64 | \n", "8.35 | \n", "8.16 | \n", "8.06 | \n", "8.08 | \n", "7.88 | \n", "... | \n", "4.83 | \n", "4.90 | \n", "5.02 | \n", "5.07 | \n", "4.88 | \n", "4.46 | \n", "4.32 | \n", "4.54 | \n", "4.59 | \n", "4.87 | \n", "
5 rows × 412 columns
\n", "