<commit_before>{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n",
        "# Creating a timeline with lines, dates, and text\n",
        "\n",
        "How to create a simple timeline using Matplotlib release dates.\n",
        "\n",
        "Timelines can be created with a collection of dates and text. In this example,\n",
        "we show how to create a simple timeline using the dates for recent releases\n",
        "of Matplotlib. First, we'll pull the data from GitHub.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[('0.1', '2010-04-16'), ('0.10', '2013-06-13'), ('0.10.1', '2013-06-14')]\n"
          ]
        }
      ],
      "source": [
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "import matplotlib.dates as mdates\n",
        "from datetime import datetime\n",
        "import sqlite3\n",
        "\n",
        "con = sqlite3.connect('main.db')\n",
        "\n",
        "\n",
        "if 0:\n",
        "    names = ['v2.2.4', 'v3.0.3', 'v3.0.2', 'v3.0.1', 'v3.0.0', 'v2.2.3',\n",
        "             'v2.2.2', 'v2.2.1', 'v2.2.0', 'v2.1.2', 'v2.1.1', 'v2.1.0',\n",
        "             'v2.0.2', 'v2.0.1', 'v2.0.0', 'v1.5.3', 'v1.5.2', 'v1.5.1',\n",
        "             'v1.5.0', 'v1.4.3', 'v1.4.2', 'v1.4.1', 'v1.4.0']\n",
        "\n",
        "    dates = ['2019-02-26', '2019-02-26', '2018-11-10', '2018-11-10',\n",
        "             '2018-09-18', '2018-08-10', '2018-03-17', '2018-03-16',\n",
        "             '2018-03-06', '2018-01-18', '2017-12-10', '2017-10-07',\n",
        "             '2017-05-10', '2017-05-02', '2017-01-17', '2016-09-09',\n",
        "             '2016-07-03', '2016-01-10', '2015-10-29', '2015-02-16',\n",
        "             '2014-10-26', '2014-10-18', '2014-08-26']\n",
        "\n",
        "    # Convert date strings (e.g. 2014-10-18) to datetime\n",
        "    dates = [datetime.strptime(d, \"%Y-%m-%d\") for d in dates]\n",
        "else:\n",
        "    project_name = 'flask'\n",
        "    sql = f'''\n",
        "    select tag, date(creator_dt)\n",
        "    from projects join releases on projects.id = releases.project_id\n",
        "    where name = '{project_name}'\n",
        "    '''\n",
        "    raw_releases = con.execute(sql).fetchall()\n",
        "    print(raw_releases[:3])\n",
        "    dates = [row[1] for row in raw_releases]\n",
        "    names = [row[0] for row in raw_releases]\n",
        "    dates = [datetime.strptime(d, \"%Y-%m-%d\") for d in dates]\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "image/png": "<KEY>//<KEY>",
            "text/plain": [
              "<Figure size 633.6x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "needs_background": "light"
          },
          "output_type": "display_data"
        }
      ],
      "source": [
        "# Choose some nice levels\n",
        "levels = np.tile([-5, 5, -3, 3, -1, 1],\n",
        "                 int(np.ceil(len(dates)/6)))[:len(dates)]\n",
        "\n",
        "# Create figure and plot a stem plot with the date\n",
        "fig, ax = plt.subplots(figsize=(8.8, 4), constrained_layout=True)\n",
        "\n",
        "ax.vlines(dates, 0, levels, color=\"tab:red\")  # The vertical stems.\n",
        "ax.plot(dates, np.zeros_like(dates), \"-o\",\n",
        "        color=\"black\", markerfacecolor=\"white\")  # Baseline and markers on it.\n",
        "\n",
        "releases = zip(dates, levels, names)\n",
        "# ax.plot(dates, np.zeros_like(dates), \"-o\",\n",
        "#         color=\"black\", markerfacecolor=\"white\")  # Baseline and markers on it.\n",
        "\n",
        "# annotate lines\n",
        "astyle = dict(textcoords=\"offset points\",\n",
        "                horizontalalignment=\"right\")\n",
        "for a_date, a_level, name in releases:\n",
        "        ax.annotate(name, xy=(a_date, a_level),\n",
        "                xytext=(-3, np.sign(a_level)*3),\n",
        "                arrowprops=dict() if name.endswith('0.0') else None,\n",
        "                verticalalignment=\"bottom\" if a_level > 0 else \"top\",\n",
        "                **astyle)\n",
        "\n",
        "if 0:\n",
        "        major_dates = [date for date,_,name in releases if name.endswith('.0.0')]\n",
        "        print(major_dates)\n",
        "\n",
        "# format xaxis with X month intervals\n",
        "ax.xaxis.set_major_locator(mdates.MonthLocator(interval=6))\n",
        "ax.xaxis.set_major_formatter(mdates.DateFormatter(\"%b %Y\"))\n",
        "plt.setp(ax.get_xticklabels(), ha=\"right\")\n",
        "# plt.setp(ax.get_xticklabels(), rotation=30, ha=\"right\")\n",
        "\n",
        "\n",
        "# remove y axis and spines\n",
        "ax.yaxis.set_visible(False)\n",
        "ax.spines[[\"left\", \"top\", \"right\"]].set_visible(False)\n",
        "ax.set(title=f\"{project_name.title()} release dates\")\n",
        "\n",
        "ax.margins(y=0.2)\n",
        "plt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",<commit_msg>put big arrow on major releases<commit_after>{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 35,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n",
        "# Creating a timeline with lines, dates, and text\n",
        "\n",
        "How to create a simple timeline using Matplotlib release dates.\n",
        "\n",
        "Timelines can be created with a collection of dates and text. In this example,\n",
        "we show how to create a simple timeline using the dates for recent releases\n",
        "of Matplotlib. First, we'll pull the data from GitHub.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 36,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[('0.1', '2010-04-16'), ('0.10', '2013-06-13'), ('0.10.1', '2013-06-14')]\n"
          ]
        }
      ],
      "source": [
        "import re\n",
        "import sqlite3\n",
        "from datetime import datetime\n",
        "\n",
        "import matplotlib.dates as mdates\n",
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "\n",
        "con = sqlite3.connect('main.db')\n",
        "\n",
        "\n",
        "if 0:\n",
        "    names = ['v2.2.4', 'v3.0.3', 'v3.0.2', 'v3.0.1', 'v3.0.0', 'v2.2.3',\n",
        "             'v2.2.2', 'v2.2.1', 'v2.2.0', 'v2.1.2', 'v2.1.1', 'v2.1.0',\n",
        "             'v2.0.2', 'v2.0.1', 'v2.0.0', 'v1.5.3', 'v1.5.2', 'v1.5.1',\n",
        "             'v1.5.0', 'v1.4.3', 'v1.4.2', 'v1.4.1', 'v1.4.0']\n",
        "\n",
        "    dates = ['2019-02-26', '2019-02-26', '2018-11-10', '2018-11-10',\n",
        "             '2018-09-18', '2018-08-10', '2018-03-17', '2018-03-16',\n",
        "             '2018-03-06', '2018-01-18', '2017-12-10', '2017-10-07',\n",
        "             '2017-05-10', '2017-05-02', '2017-01-17', '2016-09-09',\n",
        "             '2016-07-03', '2016-01-10', '2015-10-29', '2015-02-16',\n",
        "             '2014-10-26', '2014-10-18', '2014-08-26']\n",
        "\n",
        "    # Convert date strings (e.g. 2014-10-18) to datetime\n",
        "    dates = [datetime.strptime(d, \"%Y-%m-%d\") for d in dates]\n",
        "else:\n",
        "    project_name = 'flask'\n",
        "    sql = f'''\n",
        "    select tag, date(creator_dt)\n",
        "    from projects join releases on projects.id = releases.project_id\n",
        "    where name = '{project_name}'\n",
        "    '''\n",
        "    raw_releases = con.execute(sql).fetchall()\n",
        "    print(raw_releases[:3])\n",
        "    dates = [row[1] for row in raw_releases]\n",
        "    names = [row[0] for row in raw_releases]\n",
        "    dates = [datetime.strptime(d, \"%Y-%m-%d\") for d in dates]\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 39,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0.1 False\n",
            "0.10 False\n",
            "0.10.1 False\n",
            "0.11 False\n",
            "0.11.1 False\n",
            "0.12 False\n",
            "0.12.1 False\n",
            "0.12.2 False\n",
            "0.12.3 False\n",
            "0.12.4 False\n",
            "0.12.5 False\n",
            "0.2 False\n",
            "0.3 False\n",
            "0.3.1 False\n",
            "0.4 False\n",
            "0.5 False\n",
            "0.6 False\n",
            "0.6.1 False\n",
            "0.7 False\n",
            "0.7.1 False\n",
            "0.7.2 False\n",
            "0.8 False\n",
            "0.8.1 False\n",
            "0.9 False\n",
            "1.0 True\n",
            "1.0\n",
            "1.0.1 False\n",
            "1.0.2 False\n",
            "1.0.3 False\n",
            "1.0.4 False\n",
            "1.1.0 False\n",
            "1.1.1 False\n",
            "1.1.2 False\n",
            "1.1.3 False\n",
            "1.1.4 False\n",
            "2.0.0 True\n",
            "2.0.0\n",
            "2.0.0rc1 False\n",
            "2.0.0rc2 False\n",
            "2.0.1 False\n"
          ]
        },
        {
          "data": {
            "image/png": "<KEY>
            "text/plain": [
              "<Figure size 633.6x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "needs_background": "light"
          },
          "output_type": "display_data"
        }
      ],
      "source": [
        "# Choose some nice levels\n",
        "levels = np.tile([-5, 5, -3, 3, -1, 1],\n",
        "                 int(np.ceil(len(dates)/6)))[:len(dates)]\n",
        "\n",
        "# Create figure and plot a stem plot with the date\n",
        "fig, ax = plt.subplots(figsize=(8.8, 4), constrained_layout=True)\n",
        "\n",
        "ax.vlines(dates, 0, levels, color=\"tab:red\")  # The vertical stems.\n",
        "ax.plot(dates, np.zeros_like(dates), \"-o\",\n",
        "        color=\"black\", markerfacecolor=\"white\")  # Baseline and markers on it.\n",
        "\n",
        "releases = zip(dates, levels, names)\n",
        "# ax.plot(dates, np.zeros_like(dates), \"-o\",\n",
        "#         color=\"black\", markerfacecolor=\"white\")  # Baseline and markers on it.\n",
        "\n",
        "def is_major(rel):\n",
        "        return bool(re.match(r'[0-9]+\\.0\\.0$', rel) or re.match(r'[0-9]+\\.0$', rel))\n",
        "\n",
        "# annotate minor releases with lines\n",
        "if 1:\n",
        "        releases = list(zip(dates, levels, names))\n",
        "        astyle = dict(textcoords=\"offset points\",\n",
        "                        horizontalalignment=\"right\")\n",
        "        for a_date, a_level, name in releases:\n",
        "                ax.annotate(name, xy=(a_date, a_level),\n",
        "                        xytext=(-3, np.sign(a_level)*3),\n",
        "                        verticalalignment=\"bottom\" if a_level > 0 else \"top\",\n",
        "                        **astyle)\n",
        "for a_date, a_level, name in releases:\n",
        "        print(name, is_major(name))\n",
        "        if not is_major(name):\n",
        "                continue\n",
        "        print(name)\n",
        "        t = ax.text(\n",
        "        a_date, a_level, name, \n",
        "        ha=\"center\", va=\"center\", rotation=45, size=15,\n",
        "        bbox=dict(boxstyle=\"rarrow,pad=0.3\", fc=\"cyan\", ec=\"b\", lw=2))\n",
        "\n",
        "ax.xaxis.set_major_locator(mdates.MonthLocator(interval=12))\n",
        "ax.xaxis.set_major_formatter(mdates.DateFormatter(\"%Y\"))\n",
        "\n",
        "# remove y axis and spines\n",
        "ax.yaxis.set_visible(False)\n",
        "ax.spines[[\"left\", \"top\", \"right\"]].set_visible(False)\n",
        "ax.set(title=f\"{project_name.title()} release dates\")\n",
        "\n",
        "ax.margins(y=0.2)\n",
        "plt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
{'fraction_non_alphanumeric': 0.2104648617091148, 'fraction_numerical': 0.05903816682478289, 'mean_word_length': 1.466966696669667, 'pattern_counts': {'":': 91, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6159563', 'n_tokens_mistral': 5335, 'n_tokens_neox': 4903, 'n_words': 921}