{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Rose Plot.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "authorship_tag": "ABX9TyMacKCdGJk+y4pP4pJUnkc4",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/morteza/notebooks/blob/master/Rose_Plot.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns; sns.set('notebook',style='ticks')\n",
        "\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "from math import radians"
      ],
      "metadata": {
        "id": "7YsEHL9hREty"
      },
      "execution_count": 1,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Generate mock data\n",
        "\n",
        "### required columns:\n",
        "\n",
        "- `task`\n",
        "- `field_1`\n",
        "- `field_2`\n",
        "- ...\n",
        "- `field_n`"
      ],
      "metadata": {
        "id": "aeL1TzkJRFkx"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "WNB0-eDNTUnE"
      },
      "outputs": [],
      "source": [
        "n_tasks = 20\n",
        "\n",
        "# data\n",
        "data = pd.DataFrame({\n",
        "  'task': ['task ' + str(i) for i in list(range(1, n_tasks+1)) ],\n",
        "  'psychology': np.random.randint(low=1, high=10, size=n_tasks),\n",
        "  'neuroscience': np.random.randint(low=1, high=10, size=n_tasks),\n",
        "  'cognitive': np.random.randint(low=1, high=10, size=n_tasks),\n",
        "  'social': np.random.randint(low=1, high=10, size=n_tasks),\n",
        "})\n",
        "\n",
        "# sort data by one of the columns\n",
        "data = data.sort_values(by=data.columns[1], ascending=False).reset_index()"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Now define a function to plot rose plot given a propoer data frame."
      ],
      "metadata": {
        "id": "pQPyWMOSRTGp"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def rose_plot(data: pd.DataFrame):\n",
        "\n",
        "  # create the plot\n",
        "  fig, ax = plt.subplots(figsize=(10,10), subplot_kw=dict(polar=True))\n",
        "\n",
        "  ax = plt.subplot(polar=True)\n",
        "\n",
        "  polar_ticks = np.linspace(0., 2*np.pi, data['task'].nunique() + 1)\n",
        "  plt.xticks(polar_ticks[:-1], data['task'])\n",
        "\n",
        "  # If you want the first axis to be on top:\n",
        "  ax.set_theta_offset(np.pi / 2)\n",
        "  ax.set_theta_direction(-1)\n",
        "\n",
        "  # Draw ylabels\n",
        "  ax.set_rlabel_position(0)\n",
        "  ax.set_yticklabels([])\n",
        "\n",
        "  bar_width = 2 * np.pi / len(data)\n",
        "  bar_x = [i * bar_width for i in data.index]\n",
        "\n",
        "  plt.ylim(0, data.drop(columns=['task','index']).max().max() + 0.5)\n",
        "\n",
        "\n",
        "  for col in data.columns:\n",
        "    if col in ['task', 'index']:\n",
        "      continue   \n",
        "\n",
        "    bars = ax.fill(bar_x,\n",
        "                  data[col],\n",
        "                  edgecolor='white',\n",
        "                  label=col,\n",
        "                  alpha=0.5)\n",
        "\n",
        "  plt.legend()\n",
        "  plt.show()\n"
      ],
      "metadata": {
        "id": "PFXZASI8Qn0A"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# plot it\n",
        "\n",
        "rose_plot(data)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 618
        },
        "id": "w_kGa2RzQqHi",
        "outputId": "e6206008-6838-463b-90c7-ab2c48755565"
      },
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 175208 characters]\n",
            "text/plain": [
              "<Figure size 720x720 with 1 Axes>"
            ]
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    }
  ]
}