{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import random\n",
    "\n",
    "plt.rcParams[\"font.family\"] = \"Times New Roman\"\n",
    "plt.rcParams[\"font.size\"] = 16\n",
    "\n",
    "g_label_fontsize = 16\n",
    "\n",
    "colors = [\n",
    "    \"#233142\",\n",
    "    \"#F95959\",\n",
    "]\n",
    "\n",
    "edgecolors = [\n",
    "    \"#FFFFFF\",\n",
    "    \"#FFFFFF\",\n",
    "]\n",
    "\n",
    "hatches = [\"\", \"\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 49480 characters]",
      "text/plain": [
       "<Figure size 2100x700 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(\n",
    "    figsize=(7, 14 / 6), ncols=1, nrows=1, constrained_layout=True, dpi=300\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels_name_a = [\n",
    "    \"ModelC\\n1DP$\\\\times$8TP$\\\\times$8EP\",\n",
    "    \"ModelC\\n2DP$\\\\times$8TP$\\\\times$8EP\",\n",
    "    \"ModelD\\n1DP$\\\\times$8TP$\\\\times$8EP\",\n",
    "    \"ModelD\\n2DP$\\\\times$8TP$\\\\times$8EP\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# time\n",
    "data_a = {\n",
    "    labels_name_a[0]: [\n",
    "        random.uniform(190, 250) * 10,\n",
    "        random.uniform(190, 250),\n",
    "    ],  # modela 8DP\n",
    "    labels_name_a[1]: [\n",
    "        random.uniform(190, 250) * 10,\n",
    "        random.uniform(190, 250),\n",
    "    ],  # modela 8TP\n",
    "    labels_name_a[2]: [\n",
    "        random.uniform(190, 250) * 10,\n",
    "        random.uniform(190, 250),\n",
    "    ],  # modela 2DP 4TP\n",
    "    labels_name_a[3]: [\n",
    "        random.uniform(190, 250) * 10,\n",
    "        random.uniform(190, 250),\n",
    "    ],  # modela 2DP 8TP\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "legend_labels = [\"Lynx without Graph Rewrite\", \"Lynx\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "bar_width = 0.2\n",
    "group_spaing = 0.15\n",
    "\n",
    "group_positions = {}\n",
    "current_pos = 0\n",
    "\n",
    "x_label_cnt = len(data_a.keys())\n",
    "x_label_group_cnt = 2\n",
    "\n",
    "for idx in range(x_label_cnt):\n",
    "    group_positions[idx] = []\n",
    "    for _ in range(x_label_group_cnt):\n",
    "        group_positions[idx].append(current_pos)\n",
    "        current_pos += bar_width\n",
    "    current_pos += group_spaing\n",
    "\n",
    "group_centers = {}\n",
    "for idx, positions in group_positions.items():\n",
    "    group_centers[idx] = sum(positions) / len(positions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(13.333333333333332, 0.5, 'Time (s)')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_set = set()\n",
    "for idx, (x_label, y_data) in enumerate(data_a.items()):\n",
    "    positions = group_positions[idx]\n",
    "\n",
    "    for i, (pos, value, color, edgecolor, hatch, label) in enumerate(\n",
    "        zip(\n",
    "            positions,\n",
    "            y_data,\n",
    "            colors,\n",
    "            edgecolors,\n",
    "            hatches,\n",
    "            legend_labels,\n",
    "        )\n",
    "    ):\n",
    "        if label in label_set:\n",
    "            local_label = None\n",
    "        else:\n",
    "            local_label = label\n",
    "            label_set.add(local_label)\n",
    "\n",
    "        ax.bar(\n",
    "            pos,\n",
    "            value,\n",
    "            width=bar_width,\n",
    "            color=color,\n",
    "            label=local_label,\n",
    "            hatch=hatch,\n",
    "            edgecolor=edgecolor,\n",
    "        )\n",
    "\n",
    "ax.set_xticks(list(group_centers.values()))\n",
    "ax.set_xticklabels(list(data_a.keys()))\n",
    "\n",
    "ax.set_ylim(0, 3500)\n",
    "ax.set_yticks([0, 1500, 3000])\n",
    "ax.set_yticklabels([\"0\", \"1500\", \"3000\"], rotation=90, ha=\"center\", va=\"center\")\n",
    "\n",
    "ax.tick_params(axis=\"x\", bottom=False, labelsize=g_label_fontsize - 4, pad=1)\n",
    "ax.tick_params(axis=\"y\", left=True, labelsize=g_label_fontsize, pad=5)\n",
    "ax.set_ylabel(\"Time (s)\", fontsize=g_label_fontsize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f0222abb9e0>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig.legend(\n",
    "    ncol=3,\n",
    "    loc=\"upper center\",\n",
    "    frameon=False,\n",
    "    shadow=False,\n",
    "    bbox_to_anchor=(0.5, 0.95),\n",
    "    fontsize=g_label_fontsize,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 86344 characters]",
      "text/plain": [
       "<Figure size 2100x700 with 1 Axes>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig.savefig(\"subgraph_rewrite.pdf\", bbox_inches=\"tight\", dpi=1000)\n",
    "fig"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
