{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Radar charts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### A radar chart is basically a line plot with a polar projection. There are two major things we have to do. First, we have to repeat the first values at the end to close the line. Second, we have to create a subplot with polar projection. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example: Employee Evaluations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Sample employee evaluation data with attributes `Efficiency`, `Quality`, `Commitment`, `Responsible Conduct`, and `Cooperation`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "evaluations = pd.DataFrame(\n", " { 'Employee': ['Andy', 'Betty', 'Chuck', 'Diana'],\n", " 'Efficiency': [5, 4, 4, 3],\n", " 'Quality': [5, 5, 3, 3],\n", " 'Commitment': [5, 4, 4, 4],\n", " 'Responsible Conduct': [4, 4, 4, 3],\n", " 'Cooperation': [4, 3, 4, 5]\n", " }\n", ")\n", "\n", "evaluations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The maximum rating will be the y value of the outer ring." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "employee_max = evaluations.select_dtypes(include=[np.number]).max()\n", "\n", "print(f'{type(employee_max) = }')\n", "employee_max" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "max_rating = employee_max.max()\n", "max_rating" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create the angle values." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f'{evaluations.columns = }')\n", "print(f'{evaluations.values =}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "attributes = list(evaluations.columns[1:])\n", "values = list(evaluations.values[:, 1:])\n", "employees = list(evaluations.values[:, 0])\n", "\n", "angles = [n/float(len(attributes)) * 2*np.pi \n", " for n in range(len(attributes))]\n", "\n", "angles" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Close the plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "angles += angles[:1]\n", "angles" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "values = np.asarray(values)\n", "values = np.concatenate([values, values[:, 0:1]], axis=1)\n", "\n", "print(f'{angles = }')\n", "print(f'{values = }')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create subplots with polar projection. Set tight layout so nothing overlaps." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create the figure.\n", "plt.figure(figsize=(8, 8), dpi=150)\n", "\n", "# Create the subplots.\n", "for i in range(4):\n", " ax = plt.subplot(2, 2, i + 1, polar=True)\n", " ax.plot(angles, values[i])\n", " \n", " ax.set_yticks([y for y in range(1, max_rating + 1)])\n", " ax.set_xticks(angles[:len(angles) - 1])\n", " ax.set_xticklabels(attributes)\n", " ax.set_title(employees[i], fontsize=14, color='r')\n", " \n", "plt.tight_layout()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Adapted from ***Data Visualization with Python***, by Mario Döbler and Tim Großmann, Packt 2019, ISBN 978-1-78995-646-7" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (c) 2024 by Ronald Mak" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }