{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# Bar plots\n",
    "\n",
    "An example of bar plots with matplotlib.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22316 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 生成 12 根柱子\n",
    "n = 12\n",
    "X = np.arange(n)\n",
    "\n",
    "# 随机数发生器\n",
    "rng = np.random.default_rng()\n",
    "\n",
    "# 上半部分：线性递减趋势 × 随机系数 [0.5,1]\n",
    "Y1 = (1 - X / n) * rng.uniform(0.5, 1.0, n)\n",
    "# 下半部分：同样趋势，负向显示\n",
    "Y2 = (1 - X / n) * rng.uniform(0.5, 1.0, n)\n",
    "\n",
    "# 几乎铺满图窗的坐标轴\n",
    "plt.axes((0.025, 0.025, 0.95, 0.95))\n",
    "\n",
    "# 绘制正负双向柱状图\n",
    "plt.bar(X, +Y1, facecolor=\"#9999ff\", edgecolor=\"white\")\n",
    "plt.bar(X, -Y2, facecolor=\"#ff9999\", edgecolor=\"white\")\n",
    "\n",
    "# 在柱顶/柱底标注数值\n",
    "for x, y in zip(X, Y1):\n",
    "    plt.text(x, y + 0.05, f\"{y:.2f}\", ha=\"center\", va=\"bottom\")\n",
    "for x, y in zip(X, Y2):\n",
    "    plt.text(x, -y - 0.05, f\"{y:.2f}\", ha=\"center\", va=\"top\")\n",
    "\n",
    "# 去坐标轴刻度，留出边距\n",
    "plt.xlim(-0.5, n)\n",
    "plt.xticks([])\n",
    "plt.ylim(-1.25, 1.25)\n",
    "plt.yticks([])\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
