{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# Distances exercise\n",
    "\n",
    "Plot distances in a grid\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 10268 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 生成一维数组 [0,1,2,3,4]，作为后续坐标轴\n",
    "x = np.arange(5)\n",
    "\n",
    "# 生成列向量 [[0],[1],[2],[3],[4]]，形状 (5,1)，用于广播计算\n",
    "y = np.arange(5)[:, np.newaxis]\n",
    "\n",
    "# 计算每个网格点 (x_i, y_j) 到原点 (0,0) 的欧氏距离，得到 5×5 距离矩阵\n",
    "distance = np.sqrt(x**2 + y**2)\n",
    "\n",
    "# 用伪彩色图显示距离矩阵，颜色深浅对应距离大小\n",
    "plt.pcolor(distance)\n",
    "\n",
    "# 添加右侧颜色标尺，方便对照数值与颜色\n",
    "plt.colorbar()\n",
    "\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
}
