initial upload

This commit is contained in:
2025-10-21 11:20:44 +08:00
parent ad1b18ba06
commit 4333398dbe
131 changed files with 124404 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,22 @@
# year hare lynx carrot
1900 30e3 4e3 48300
1901 47.2e3 6.1e3 48200
1902 70.2e3 9.8e3 41500
1903 77.4e3 35.2e3 38200
1904 36.3e3 59.4e3 40600
1905 20.6e3 41.7e3 39800
1906 18.1e3 19e3 38600
1907 21.4e3 13e3 42300
1908 22e3 8.3e3 44500
1909 25.4e3 9.1e3 42100
1910 27.1e3 7.4e3 46000
1911 40.3e3 8e3 46800
1912 57e3 12.3e3 43800
1913 76.6e3 19.5e3 40900
1914 52.3e3 45.7e3 39400
1915 19.5e3 51.1e3 39000
1916 11.2e3 29.7e3 36700
1917 7.6e3 15.8e3 41800
1918 14.6e3 9.7e3 43300
1919 16.2e3 10.1e3 41300
1920 24.7e3 8.6e3 47300

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Exercise 1\n\nSolution of the exercise 1 with matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nn = 256\nX = np.linspace(-np.pi, np.pi, 256)\nC, S = np.cos(X), np.sin(X)\nplt.plot(X, C)\nplt.plot(X, S)\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Exercise\n\nExercises with matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(8, 5), dpi=80)\nplt.subplot(111)\n\nX = np.linspace(-np.pi, np.pi, 256)\nC, S = np.cos(X), np.sin(X)\n\nplt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\", label=\"cosine\")\nplt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\", label=\"sine\")\n\nax = plt.gca()\nax.spines[\"right\"].set_color(\"none\")\nax.spines[\"top\"].set_color(\"none\")\nax.xaxis.set_ticks_position(\"bottom\")\nax.spines[\"bottom\"].set_position((\"data\", 0))\nax.yaxis.set_ticks_position(\"left\")\nax.spines[\"left\"].set_position((\"data\", 0))\n\nplt.xlim(X.min() * 1.1, X.max() * 1.1)\nplt.xticks(\n [-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],\n [r\"$-\\pi$\", r\"$-\\pi/2$\", r\"$0$\", r\"$+\\pi/2$\", r\"$+\\pi$\"],\n)\n\nplt.ylim(C.min() * 1.1, C.max() * 1.1)\nplt.yticks([-1, 1], [r\"$-1$\", r\"$+1$\"])\n\nplt.legend(loc=\"upper left\")\n\nt = 2 * np.pi / 3\nplt.plot([t, t], [0, np.cos(t)], color=\"blue\", linewidth=1.5, linestyle=\"--\")\nplt.scatter(\n [\n t,\n ],\n [\n np.cos(t),\n ],\n 50,\n color=\"blue\",\n)\nplt.annotate(\n r\"$sin(\\frac{2\\pi}{3})=\\frac{\\sqrt{3}}{2}$\",\n xy=(t, np.sin(t)),\n xycoords=\"data\",\n xytext=(10, 30),\n textcoords=\"offset points\",\n fontsize=16,\n arrowprops={\"arrowstyle\": \"->\", \"connectionstyle\": \"arc3,rad=.2\"},\n)\n\nplt.plot([t, t], [0, np.sin(t)], color=\"red\", linewidth=1.5, linestyle=\"--\")\nplt.scatter(\n [\n t,\n ],\n [\n np.sin(t),\n ],\n 50,\n color=\"red\",\n)\nplt.annotate(\n r\"$cos(\\frac{2\\pi}{3})=-\\frac{1}{2}$\",\n xy=(t, np.cos(t)),\n xycoords=\"data\",\n xytext=(-90, -50),\n textcoords=\"offset points\",\n fontsize=16,\n arrowprops={\"arrowstyle\": \"->\", \"connectionstyle\": \"arc3,rad=.2\"},\n)\n\nfor label in ax.get_xticklabels() + ax.get_yticklabels():\n label.set_fontsize(16)\n label.set_bbox({\"facecolor\": \"white\", \"edgecolor\": \"None\", \"alpha\": 0.65})\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Exercise 2\n\nExercise 2 with matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\n# Create a new figure of size 8x6 points, using 100 dots per inch\nplt.figure(figsize=(8, 6), dpi=80)\n\n# Create a new subplot from a grid of 1x1\nplt.subplot(111)\n\nX = np.linspace(-np.pi, np.pi, 256)\nC, S = np.cos(X), np.sin(X)\n\n# Plot cosine using blue color with a continuous line of width 1 (pixels)\nplt.plot(X, C, color=\"blue\", linewidth=1.0, linestyle=\"-\")\n\n# Plot sine using green color with a continuous line of width 1 (pixels)\nplt.plot(X, S, color=\"green\", linewidth=1.0, linestyle=\"-\")\n\n# Set x limits\nplt.xlim(-4.0, 4.0)\n\n# Set x ticks\nplt.xticks(np.linspace(-4, 4, 9))\n\n# Set y limits\nplt.ylim(-1.0, 1.0)\n\n# Set y ticks\nplt.yticks(np.linspace(-1, 1, 5))\n\n# Show result on screen\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Exercise 3\n\nExercise 3 with matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(8, 5), dpi=80)\nplt.subplot(111)\n\nX = np.linspace(-np.pi, np.pi, 256)\nC, S = np.cos(X), np.sin(X)\n\nplt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\")\nplt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\")\n\nplt.xlim(-4.0, 4.0)\nplt.xticks(np.linspace(-4, 4, 9))\n\nplt.ylim(-1.0, 1.0)\nplt.yticks(np.linspace(-1, 1, 5))\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Exercise 4\n\nExercise 4 with matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(8, 5), dpi=80)\nplt.subplot(111)\n\nX = np.linspace(-np.pi, np.pi, 256)\nS = np.sin(X)\nC = np.cos(X)\n\nplt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\")\nplt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\")\n\nplt.xlim(X.min() * 1.1, X.max() * 1.1)\nplt.ylim(C.min() * 1.1, C.max() * 1.1)\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Exercise 5\n\nExercise 5 with matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(8, 5), dpi=80)\nplt.subplot(111)\n\nX = np.linspace(-np.pi, np.pi, 256)\nS = np.sin(X)\nC = np.cos(X)\n\nplt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\")\nplt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\")\n\nplt.xlim(X.min() * 1.1, X.max() * 1.1)\nplt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi])\n\nplt.ylim(C.min() * 1.1, C.max() * 1.1)\nplt.yticks([-1, 0, +1])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Exercise 6\n\nExercise 6 with matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(8, 5), dpi=80)\nplt.subplot(111)\n\nX = np.linspace(-np.pi, np.pi, 256)\nC = np.cos(X)\nS = np.sin(X)\n\nplt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\")\nplt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\")\n\nplt.xlim(X.min() * 1.1, X.max() * 1.1)\nplt.xticks(\n [-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],\n [r\"$-\\pi$\", r\"$-\\pi/2$\", r\"$0$\", r\"$+\\pi/2$\", r\"$+\\pi$\"],\n)\n\nplt.ylim(C.min() * 1.1, C.max() * 1.1)\nplt.yticks([-1, 0, +1], [r\"$-1$\", r\"$0$\", r\"$+1$\"])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Exercise 7\n\nExercise 7 with matplotlib\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(8, 5), dpi=80)\nplt.subplot(111)\n\nX = np.linspace(-np.pi, np.pi, 256, endpoint=True)\nC = np.cos(X)\nS = np.sin(X)\n\nplt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\")\nplt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\")\n\nax = plt.gca()\nax.spines[\"right\"].set_color(\"none\")\nax.spines[\"top\"].set_color(\"none\")\nax.xaxis.set_ticks_position(\"bottom\")\nax.spines[\"bottom\"].set_position((\"data\", 0))\nax.yaxis.set_ticks_position(\"left\")\nax.spines[\"left\"].set_position((\"data\", 0))\n\nplt.xlim(X.min() * 1.1, X.max() * 1.1)\nplt.xticks(\n [-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],\n [r\"$-\\pi$\", r\"$-\\pi/2$\", r\"$0$\", r\"$+\\pi/2$\", r\"$+\\pi$\"],\n)\n\nplt.ylim(C.min() * 1.1, C.max() * 1.1)\nplt.yticks([-1, 0, +1], [r\"$-1$\", r\"$0$\", r\"$+1$\"])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Exercise 8\n\nExercise 8 with matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(8, 5), dpi=80)\nplt.subplot(111)\n\nX = np.linspace(-np.pi, np.pi, 256, endpoint=True)\nC = np.cos(X)\nS = np.sin(X)\n\nplt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\", label=\"cosine\")\nplt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\", label=\"sine\")\n\nax = plt.gca()\nax.spines[\"right\"].set_color(\"none\")\nax.spines[\"top\"].set_color(\"none\")\nax.xaxis.set_ticks_position(\"bottom\")\nax.spines[\"bottom\"].set_position((\"data\", 0))\nax.yaxis.set_ticks_position(\"left\")\nax.spines[\"left\"].set_position((\"data\", 0))\n\nplt.xlim(X.min() * 1.1, X.max() * 1.1)\nplt.xticks(\n [-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],\n [r\"$-\\pi$\", r\"$-\\pi/2$\", r\"$0$\", r\"$+\\pi/2$\", r\"$+\\pi$\"],\n)\n\nplt.ylim(C.min() * 1.1, C.max() * 1.1)\nplt.yticks([-1, +1], [r\"$-1$\", r\"$+1$\"])\n\nplt.legend(loc=\"upper left\")\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Exercise 9\n\nExercise 9 with matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(8, 5), dpi=80)\nplt.subplot(111)\n\nX = np.linspace(-np.pi, np.pi, 256, endpoint=True)\nC = np.cos(X)\nS = np.sin(X)\n\nplt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\", label=\"cosine\")\nplt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\", label=\"sine\")\n\nax = plt.gca()\nax.spines[\"right\"].set_color(\"none\")\nax.spines[\"top\"].set_color(\"none\")\nax.xaxis.set_ticks_position(\"bottom\")\nax.spines[\"bottom\"].set_position((\"data\", 0))\nax.yaxis.set_ticks_position(\"left\")\nax.spines[\"left\"].set_position((\"data\", 0))\n\nplt.xlim(X.min() * 1.1, X.max() * 1.1)\nplt.xticks(\n [-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],\n [r\"$-\\pi$\", r\"$-\\pi/2$\", r\"$0$\", r\"$+\\pi/2$\", r\"$+\\pi$\"],\n)\n\nplt.ylim(C.min() * 1.1, C.max() * 1.1)\nplt.yticks([-1, +1], [r\"$-1$\", r\"$+1$\"])\n\nt = 2 * np.pi / 3\nplt.plot([t, t], [0, np.cos(t)], color=\"blue\", linewidth=1.5, linestyle=\"--\")\nplt.scatter(\n [\n t,\n ],\n [\n np.cos(t),\n ],\n 50,\n color=\"blue\",\n)\nplt.annotate(\n r\"$sin(\\frac{2\\pi}{3})=\\frac{\\sqrt{3}}{2}$\",\n xy=(t, np.sin(t)),\n xycoords=\"data\",\n xytext=(+10, +30),\n textcoords=\"offset points\",\n fontsize=16,\n arrowprops={\"arrowstyle\": \"->\", \"connectionstyle\": \"arc3,rad=.2\"},\n)\n\nplt.plot([t, t], [0, np.sin(t)], color=\"red\", linewidth=1.5, linestyle=\"--\")\nplt.scatter(\n [\n t,\n ],\n [\n np.sin(t),\n ],\n 50,\n color=\"red\",\n)\nplt.annotate(\n r\"$cos(\\frac{2\\pi}{3})=-\\frac{1}{2}$\",\n xy=(t, np.cos(t)),\n xycoords=\"data\",\n xytext=(-90, -50),\n textcoords=\"offset points\",\n fontsize=16,\n arrowprops={\"arrowstyle\": \"->\", \"connectionstyle\": \"arc3,rad=.2\"},\n)\n\nplt.legend(loc=\"upper left\")\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Aliased versus anti-aliased\n\nThis example demonstrates aliased versus anti-aliased text.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nsize = 128, 16\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\n\nplt.axes((0, 0, 1, 1), frameon=False)\n\nplt.rcParams[\"text.antialiased\"] = False\nplt.text(0.5, 0.5, \"Aliased\", ha=\"center\", va=\"center\")\n\nplt.xlim(0, 1)\nplt.ylim(0, 1)\nplt.xticks([])\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Alpha: transparency\n\nThis example demonstrates using alpha for transparency.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nsize = 256, 16\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\nplt.axes((0, 0.1, 1, 0.8), frameon=False)\n\nfor i in range(1, 11):\n plt.axvline(i, linewidth=1, color=\"blue\", alpha=0.25 + 0.75 * i / 10.0)\n\nplt.xlim(0, 11)\nplt.xticks([])\nplt.yticks([])\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Aliased versus anti-aliased\n\nThe example shows aliased versus anti-aliased text.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nsize = 128, 16\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\nplt.axes((0, 0, 1, 1), frameon=False)\n\nplt.rcParams[\"text.antialiased\"] = True\nplt.text(0.5, 0.5, \"Anti-aliased\", ha=\"center\", va=\"center\")\n\nplt.xlim(0, 1)\nplt.ylim(0, 1)\nplt.xticks([])\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# The colors matplotlib line plots\n\nAn example demoing the various colors taken by matplotlib's plot.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nsize = 256, 16\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\nplt.axes((0, 0.1, 1, 0.8), frameon=False)\n\nfor i in range(1, 11):\n plt.plot([i, i], [0, 1], lw=1.5)\n\nplt.xlim(0, 11)\nplt.xticks([])\nplt.yticks([])\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Colormaps\n\nAn example plotting the matplotlib colormaps.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n\nimport matplotlib\nimport matplotlib.pyplot as plt\n\n\nplt.rc(\"text\", usetex=False)\na = np.outer(np.arange(0, 1, 0.01), np.ones(10))\n\nplt.figure(figsize=(10, 5))\nplt.subplots_adjust(top=0.8, bottom=0.05, left=0.01, right=0.99)\nmaps = [m for m in matplotlib.colormaps if not m.endswith(\"_r\")]\nmaps.sort()\nl = len(maps) + 1\n\nfor i, m in enumerate(maps):\n plt.subplot(1, l, i + 1)\n plt.axis(\"off\")\n plt.imshow(a, aspect=\"auto\", cmap=plt.get_cmap(m), origin=\"lower\")\n plt.title(m, rotation=90, fontsize=10, va=\"bottom\")\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Dash capstyle\n\nAn example demoing the dash capstyle.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nsize = 256, 16\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\nplt.axes((0, 0, 1, 1), frameon=False)\n\nplt.plot(\n np.arange(4),\n np.ones(4),\n color=\"blue\",\n dashes=[15, 15],\n linewidth=8,\n dash_capstyle=\"butt\",\n)\n\nplt.plot(\n 5 + np.arange(4),\n np.ones(4),\n color=\"blue\",\n dashes=[15, 15],\n linewidth=8,\n dash_capstyle=\"round\",\n)\n\nplt.plot(\n 10 + np.arange(4),\n np.ones(4),\n color=\"blue\",\n dashes=[15, 15],\n linewidth=8,\n dash_capstyle=\"projecting\",\n)\n\nplt.xlim(0, 14)\nplt.xticks([])\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Dash join style\n\nExample demoing the dash join style.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nsize = 256, 16\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\nplt.axes((0, 0, 1, 1), frameon=False)\n\nplt.plot(\n np.arange(3),\n [0, 1, 0],\n color=\"blue\",\n dashes=[12, 5],\n linewidth=8,\n dash_joinstyle=\"miter\",\n)\nplt.plot(\n 4 + np.arange(3),\n [0, 1, 0],\n color=\"blue\",\n dashes=[12, 5],\n linewidth=8,\n dash_joinstyle=\"bevel\",\n)\nplt.plot(\n 8 + np.arange(3),\n [0, 1, 0],\n color=\"blue\",\n dashes=[12, 5],\n linewidth=8,\n dash_joinstyle=\"round\",\n)\n\nplt.xlim(0, 12)\nplt.ylim(-1, 2)\nplt.xticks([])\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Linestyles\n\nPlot the different line styles.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\n\ndef linestyle(ls, i):\n X = i * 0.5 * np.ones(11)\n Y = np.arange(11)\n plt.plot(\n X,\n Y,\n ls,\n color=(0.0, 0.0, 1, 1),\n lw=3,\n ms=8,\n mfc=(0.75, 0.75, 1, 1),\n mec=(0, 0, 1, 1),\n )\n plt.text(0.5 * i, 10.25, ls, rotation=90, fontsize=15, va=\"bottom\")\n\n\nlinestyles = [\n \"-\",\n \"--\",\n \":\",\n \"-.\",\n \".\",\n \",\",\n \"o\",\n \"^\",\n \"v\",\n \"<\",\n \">\",\n \"s\",\n \"+\",\n \"x\",\n \"d\",\n \"1\",\n \"2\",\n \"3\",\n \"4\",\n \"h\",\n \"p\",\n \"|\",\n \"_\",\n \"D\",\n \"H\",\n]\nn_lines = len(linestyles)\n\nsize = 20 * n_lines, 300\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nplt.axes((0, 0.01, 1, 0.9), frameon=False)\n\nfor i, ls in enumerate(linestyles):\n linestyle(ls, i)\n\nplt.xlim(-0.2, 0.2 + 0.5 * n_lines)\nplt.xticks([])\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Linewidth\n\nPlot various linewidth with matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nsize = 256, 16\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\nplt.axes((0, 0.1, 1, 0.8), frameon=False)\n\nfor i in range(1, 11):\n plt.plot([i, i], [0, 1], color=\"b\", lw=i / 2.0)\n\nplt.xlim(0, 11)\nplt.ylim(0, 1)\nplt.xticks([])\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Markers\n\nShow the different markers of matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\n\ndef marker(m, i):\n X = i * 0.5 * np.ones(11)\n Y = np.arange(11)\n\n plt.plot(X, Y, lw=1, marker=m, ms=10, mfc=(0.75, 0.75, 1, 1), mec=(0, 0, 1, 1))\n plt.text(0.5 * i, 10.25, repr(m), rotation=90, fontsize=15, va=\"bottom\")\n\n\nmarkers = [\n 0,\n 1,\n 2,\n 3,\n 4,\n 5,\n 6,\n 7,\n \"o\",\n \"h\",\n \"_\",\n \"1\",\n \"2\",\n \"3\",\n \"4\",\n \"8\",\n \"p\",\n \"^\",\n \"v\",\n \"<\",\n \">\",\n \"|\",\n \"d\",\n \",\",\n \"+\",\n \"s\",\n \"*\",\n \"|\",\n \"x\",\n \"D\",\n \"H\",\n \".\",\n]\n\nn_markers = len(markers)\n\nsize = 20 * n_markers, 300\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nplt.axes((0, 0.01, 1, 0.9), frameon=False)\n\nfor i, m in enumerate(markers):\n marker(m, i)\n\nplt.xlim(-0.2, 0.2 + 0.5 * n_markers)\nplt.xticks([])\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Marker edge color\n\nDemo the marker edge color of matplotlib's markers.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nsize = 256, 16\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\nplt.axes((0, 0, 1, 1), frameon=False)\n\nrng = np.random.default_rng()\n\nfor i in range(1, 11):\n r, g, b = np.random.uniform(0, 1, 3)\n plt.plot(\n [\n i,\n ],\n [\n 1,\n ],\n \"s\",\n markersize=5,\n markerfacecolor=\"w\",\n markeredgewidth=1.5,\n markeredgecolor=(r, g, b, 1),\n )\n\nplt.xlim(0, 11)\nplt.xticks([])\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Marker edge width\n\nDemo the marker edge widths of matplotlib's markers.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nsize = 256, 16\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\nplt.axes((0, 0, 1, 1), frameon=False)\n\nfor i in range(1, 11):\n plt.plot(\n [\n i,\n ],\n [\n 1,\n ],\n \"s\",\n markersize=5,\n markeredgewidth=1 + i / 10.0,\n markeredgecolor=\"k\",\n markerfacecolor=\"w\",\n )\nplt.xlim(0, 11)\nplt.xticks([])\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Marker face color\n\nDemo the marker face color of matplotlib's markers.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nsize = 256, 16\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\nplt.axes((0, 0, 1, 1), frameon=False)\n\nrng = np.random.default_rng()\n\nfor i in range(1, 11):\n r, g, b = np.random.uniform(0, 1, 3)\n plt.plot(\n [\n i,\n ],\n [\n 1,\n ],\n \"s\",\n markersize=8,\n markerfacecolor=(r, g, b, 1),\n markeredgewidth=0.1,\n markeredgecolor=(0, 0, 0, 0.5),\n )\nplt.xlim(0, 11)\nplt.xticks([])\nplt.yticks([])\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Marker size\n\nDemo the marker size control in matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nsize = 256, 16\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\nplt.axes((0, 0, 1, 1), frameon=False)\n\nfor i in range(1, 11):\n plt.plot(\n [\n i,\n ],\n [\n 1,\n ],\n \"s\",\n markersize=i,\n markerfacecolor=\"w\",\n markeredgewidth=0.5,\n markeredgecolor=\"k\",\n )\n\nplt.xlim(0, 11)\nplt.xticks([])\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Solid cap style\n\nAn example demoing the solide cap style in matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nsize = 256, 16\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\nplt.axes((0, 0, 1, 1), frameon=False)\n\nplt.plot(np.arange(4), np.ones(4), color=\"blue\", linewidth=8, solid_capstyle=\"butt\")\n\nplt.plot(\n 5 + np.arange(4), np.ones(4), color=\"blue\", linewidth=8, solid_capstyle=\"round\"\n)\n\nplt.plot(\n 10 + np.arange(4),\n np.ones(4),\n color=\"blue\",\n linewidth=8,\n solid_capstyle=\"projecting\",\n)\n\nplt.xlim(0, 14)\nplt.xticks([])\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Solid joint style\n\nAn example showing the different solid joint styles in matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nsize = 256, 16\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\nplt.axes((0, 0, 1, 1), frameon=False)\n\nplt.plot(np.arange(3), [0, 1, 0], color=\"blue\", linewidth=8, solid_joinstyle=\"miter\")\nplt.plot(\n 4 + np.arange(3), [0, 1, 0], color=\"blue\", linewidth=8, solid_joinstyle=\"bevel\"\n)\nplt.plot(\n 8 + np.arange(3), [0, 1, 0], color=\"blue\", linewidth=8, solid_joinstyle=\"round\"\n)\n\nplt.xlim(0, 12)\nplt.ylim(-1, 2)\nplt.xticks([])\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Locators for tick on axis\n\nAn example demoing different locators to position ticks on axis for\nmatplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n\nfrom matplotlib import ticker\nimport matplotlib.pyplot as plt\n\n\ndef tickline():\n plt.xlim(0, 10), plt.ylim(-1, 1), plt.yticks([])\n ax = plt.gca()\n ax.spines[\"right\"].set_color(\"none\")\n ax.spines[\"left\"].set_color(\"none\")\n ax.spines[\"top\"].set_color(\"none\")\n ax.xaxis.set_ticks_position(\"bottom\")\n ax.spines[\"bottom\"].set_position((\"data\", 0))\n ax.yaxis.set_ticks_position(\"none\")\n ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1))\n ax.plot(np.arange(11), np.zeros(11))\n return ax\n\n\nlocators = [\n \"ticker.NullLocator()\",\n \"ticker.MultipleLocator(1.0)\",\n \"ticker.FixedLocator([0, 2, 8, 9, 10])\",\n \"ticker.IndexLocator(3, 1)\",\n \"ticker.LinearLocator(5)\",\n \"ticker.LogLocator(2, [1.0])\",\n \"ticker.AutoLocator()\",\n]\n\nn_locators = len(locators)\n\nsize = 512, 40 * n_locators\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\n\n\nfor i, locator in enumerate(locators):\n plt.subplot(n_locators, 1, i + 1)\n ax = tickline()\n ax.xaxis.set_major_locator(eval(locator))\n plt.text(5, 0.3, locator[7:], ha=\"center\")\n\nplt.subplots_adjust(bottom=0.01, top=0.99, left=0.01, right=0.99)\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,87 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Subplots\n",
"\n",
"Show multiple subplots in matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAnQAAAHbCAYAAABCywdpAAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjYsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvq6yFwwAAAAlwSFlzAAAPYQAAD2EBqD+naQAACkdJREFUeJzt27Fuq1gUQNGLlRant8L/f1gkPsD0ZopRUo0iojw/Z3vWqq/QKTjSxoZp3/d9AACQdXr0AAAA/IygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABD3cuTQ7XYb67qOeZ7HNE33ngkA4H9v3/exbdu4XC7jdPr6N7hDQbeu61iW5Y8MBwDAce/v7+Pt7e3LM4eCbp7nzwuez+efTwYAwJeu1+tYluWzw75yKOg+/mY9n8+CDgDgLzryupuPIgAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIh7OXJo3/cxxhjX6/WuwwAA8K+P7vrosK8cCrpt28YYYyzL8oOxAAD4rm3bxuvr65dnpv1A9t1ut7Gu65jneUzT9McGBADgv+37PrZtG5fLZZxOX78ldyjoAAD4vXwUAQAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAg7uXIodvtNtZ1HfM8j2ma7j0T3M2+72PbtnG5XMbp9PjnGbvFM7BXcB/f2a1DQbeu61iW5Y8MB7/B+/v7eHt7e/QYdounYq/gPo7s1qGgm+f584Ln8/nnk8GDXK/XsSzL5z39aHaLZ2Cv4D6+s1uHgu7jJ+vz+Ww5eAq/5W8Yu8UzsVdwH0d26/EvOwAA8COCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDECToAgDhBBwAQJ+gAAOIEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIA4QQcAECfoAADiBB0AQJygAwCIE3QAAHGCDgAgTtABAMQJOgCAOEEHABAn6AAA4gQdAECcoAMAiBN0AABxgg4AIE7QAQDEvRw5tO/7GGOM6/V612Hg3j7u4Y97+tHsFs/AXsF9fGe3DgXdtm1jjDGWZfnBWPB7bNs2Xl9fHz2G3eKp2Cu4jyO7Ne0Hsu92u411Xcc8z2Oapj82IPxt+76PbdvG5XIZp9Pj3ziwWzwDewX38Z3dOhR0AAD8Xo9/lAIA4EcEHQBAnKADAIgTdAAAcYIOACBO0AEAxAk6AIC4fwDtDPHesVMI6QAAAABJRU5ErkJggg==",
"text/plain": [
"<Figure size 640x480 with 4 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"fig = plt.figure()\n",
"fig.subplots_adjust(bottom=0.025, left=0.025, top=0.975, right=0.975)\n",
"\n",
"plt.subplot(2, 1, 1)\n",
"plt.xticks([]), plt.yticks([])\n",
"\n",
"plt.subplot(2, 3, 4)\n",
"plt.xticks([])\n",
"plt.yticks([])\n",
"\n",
"plt.subplot(2, 3, 5)\n",
"plt.xticks([])\n",
"plt.yticks([])\n",
"\n",
"plt.subplot(2, 3, 6)\n",
"plt.xticks([])\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
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Plotting a vector field: quiver\n\nA simple example showing how to plot a vector field (quiver) with\nmatplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nn = 8\nX, Y = np.mgrid[0:n, 0:n]\nT = np.arctan2(Y - n / 2.0, X - n / 2.0)\nR = 10 + np.sqrt((Y - n / 2.0) ** 2 + (X - n / 2.0) ** 2)\nU, V = R * np.cos(T), R * np.sin(T)\n\nplt.axes((0.025, 0.025, 0.95, 0.95))\nplt.quiver(X, Y, U, V, R, alpha=0.5)\nplt.quiver(X, Y, U, V, edgecolor=\"k\", facecolor=\"None\", linewidth=0.5)\n\nplt.xlim(-1, n)\nplt.xticks([])\nplt.ylim(-1, n)\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Subplot grid\n\nAn example showing the subplot grid in matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(6, 4))\nplt.subplot(2, 2, 1)\nplt.xticks([])\nplt.yticks([])\nplt.text(0.5, 0.5, \"subplot(2,2,1)\", ha=\"center\", va=\"center\", size=20, alpha=0.5)\n\nplt.subplot(2, 2, 2)\nplt.xticks([])\nplt.yticks([])\nplt.text(0.5, 0.5, \"subplot(2,2,2)\", ha=\"center\", va=\"center\", size=20, alpha=0.5)\n\nplt.subplot(2, 2, 3)\nplt.xticks([])\nplt.yticks([])\n\nplt.text(0.5, 0.5, \"subplot(2,2,3)\", ha=\"center\", va=\"center\", size=20, alpha=0.5)\n\nplt.subplot(2, 2, 4)\nplt.xticks([])\nplt.yticks([])\nplt.text(0.5, 0.5, \"subplot(2,2,4)\", ha=\"center\", va=\"center\", size=20, alpha=0.5)\n\nplt.tight_layout()\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Horizontal arrangement of subplots\n\nAn example showing horizontal arrangement of subplots with matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(6, 4))\nplt.subplot(2, 1, 1)\nplt.xticks([])\nplt.yticks([])\nplt.text(0.5, 0.5, \"subplot(2,1,1)\", ha=\"center\", va=\"center\", size=24, alpha=0.5)\n\nplt.subplot(2, 1, 2)\nplt.xticks([])\nplt.yticks([])\nplt.text(0.5, 0.5, \"subplot(2,1,2)\", ha=\"center\", va=\"center\", size=24, alpha=0.5)\n\nplt.tight_layout()\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Subplot plot arrangement vertical\n\nAn example showing vertical arrangement of subplots with matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\n\nplt.figure(figsize=(6, 4))\nplt.subplot(1, 2, 1)\nplt.xticks([])\nplt.yticks([])\nplt.text(0.5, 0.5, \"subplot(1,2,1)\", ha=\"center\", va=\"center\", size=24, alpha=0.5)\n\nplt.subplot(1, 2, 2)\nplt.xticks([])\nplt.yticks([])\nplt.text(0.5, 0.5, \"subplot(1,2,2)\", ha=\"center\", va=\"center\", size=24, alpha=0.5)\n\nplt.tight_layout()\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Demo text printing\n\nA example showing off elaborate text printing with matplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\n\neqs = []\neqs.append(\n r\"$W^{3\\beta}_{\\delta_1 \\rho_1 \\sigma_2} = U^{3\\beta}_{\\delta_1 \\rho_1} + \\frac{1}{8 \\pi 2} \\int^{\\alpha_2}_{\\alpha_2} d \\alpha^\\prime_2 \\left[\\frac{ U^{2\\beta}_{\\delta_1 \\rho_1} - \\alpha^\\prime_2U^{1\\beta}_{\\rho_1 \\sigma_2} }{U^{0\\beta}_{\\rho_1 \\sigma_2}}\\right]$\"\n)\neqs.append(\n r\"$\\frac{d\\rho}{d t} + \\rho \\vec{v}\\cdot\\nabla\\vec{v} = -\\nabla p + \\mu\\nabla^2 \\vec{v} + \\rho \\vec{g}$\"\n)\neqs.append(r\"$\\int_{-\\infty}^\\infty e^{-x^2}dx=\\sqrt{\\pi}$\")\neqs.append(r\"$E = mc^2 = \\sqrt{{m_0}^2c^4 + p^2c^2}$\")\neqs.append(r\"$F_G = G\\frac{m_1m_2}{r^2}$\")\n\nplt.axes((0.025, 0.025, 0.95, 0.95))\n\nrng = np.random.default_rng()\n\nfor i in range(24):\n index = rng.integers(0, len(eqs))\n eq = eqs[index]\n size = np.random.uniform(12, 32)\n x, y = np.random.uniform(0, 1, 2)\n alpha = np.random.uniform(0.25, 0.75)\n plt.text(\n x,\n y,\n eq,\n ha=\"center\",\n va=\"center\",\n color=\"#11557c\",\n alpha=alpha,\n transform=plt.gca().transAxes,\n fontsize=size,\n clip_on=True,\n )\nplt.xticks([])\nplt.yticks([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Bar plot advanced\n\nAn more elaborate bar plot example\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nn = 16\nX = np.arange(n)\nY1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)\nY2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)\nplt.bar(X, Y1, facecolor=\"#9999ff\", edgecolor=\"white\")\nplt.bar(X, -Y2, facecolor=\"#ff9999\", edgecolor=\"white\")\nplt.xlim(-0.5, n)\nplt.xticks([])\nplt.ylim(-1, 1)\nplt.yticks([])\n\n\n# Add a title and a box around it\nfrom matplotlib.patches import FancyBboxPatch\n\nax = plt.gca()\nax.add_patch(\n FancyBboxPatch(\n (-0.05, 0.87),\n width=0.66,\n height=0.165,\n clip_on=False,\n boxstyle=\"square,pad=0\",\n zorder=3,\n facecolor=\"white\",\n alpha=1.0,\n transform=plt.gca().transAxes,\n )\n)\n\nplt.text(\n -0.05,\n 1.02,\n \" Bar Plot: plt.bar(...)\\n\",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"xx-large\",\n transform=plt.gca().transAxes,\n)\n\nplt.text(\n -0.05,\n 1.01,\n \"\\n\\n Make a bar plot with rectangles \",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"large\",\n transform=plt.gca().transAxes,\n)\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Boxplot with matplotlib\n\nAn example of doing box plots with matplotlib\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\n\nfig = plt.figure(figsize=(8, 5))\naxes = plt.subplot(111)\n\nn = 5\nZ = np.zeros((n, 4))\nX = np.linspace(0, 2, n)\nrng = np.random.default_rng()\nY = rng.random((n, 4))\nplt.boxplot(Y)\n\nplt.xticks([])\nplt.yticks([])\n\n\n# Add a title and a box around it\nfrom matplotlib.patches import FancyBboxPatch\n\nax = plt.gca()\nax.add_patch(\n FancyBboxPatch(\n (-0.05, 0.87),\n width=0.66,\n height=0.165,\n clip_on=False,\n boxstyle=\"square,pad=0\",\n zorder=3,\n facecolor=\"white\",\n alpha=1.0,\n transform=plt.gca().transAxes,\n )\n)\n\nplt.text(\n -0.05,\n 1.02,\n \" Box Plot: plt.boxplot(...)\\n \",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"xx-large\",\n transform=axes.transAxes,\n)\n\nplt.text(\n -0.04,\n 0.98,\n \"\\n Make a box and whisker plot \",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"large\",\n transform=axes.transAxes,\n)\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Display the contours of a function\n\nAn example demoing how to plot the contours of a function, with\nadditional layout tweaks.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\n\ndef f(x, y):\n return (1 - x / 2 + x**5 + y**3) * np.exp(-(x**2) - y**2)\n\n\nn = 256\nx = np.linspace(-3, 3, n)\ny = np.linspace(-3, 3, n)\nX, Y = np.meshgrid(x, y)\n\nplt.contourf(X, Y, f(X, Y), 8, alpha=0.75, cmap=\"hot\")\nC = plt.contour(X, Y, f(X, Y), 8, colors=\"black\", linewidth=0.5)\nplt.clabel(C, inline=1, fontsize=10)\nplt.xticks([])\nplt.yticks([])\n\n\n# Add a title and a box around it\nfrom matplotlib.patches import FancyBboxPatch\n\nax = plt.gca()\nax.add_patch(\n FancyBboxPatch(\n (-0.05, 0.87),\n width=0.66,\n height=0.165,\n clip_on=False,\n boxstyle=\"square,pad=0\",\n zorder=3,\n facecolor=\"white\",\n alpha=1.0,\n transform=plt.gca().transAxes,\n )\n)\n\nplt.text(\n -0.05,\n 1.02,\n \" Contour Plot: plt.contour(..)\\n\",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"xx-large\",\n transform=plt.gca().transAxes,\n)\n\nplt.text(\n -0.05,\n 1.01,\n \"\\n\\n Draw contour lines and filled contours \",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"large\",\n transform=plt.gca().transAxes,\n)\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Grid elaborate\n\nAn example displaying a grid on the axes and tweaking the layout.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\nfrom matplotlib.ticker import MultipleLocator\n\nfig = plt.figure(figsize=(8, 6), dpi=72, facecolor=\"white\")\naxes = plt.subplot(111)\naxes.set_xlim(0, 4)\naxes.set_ylim(0, 3)\n\naxes.xaxis.set_major_locator(MultipleLocator(1.0))\naxes.xaxis.set_minor_locator(MultipleLocator(0.1))\naxes.yaxis.set_major_locator(MultipleLocator(1.0))\naxes.yaxis.set_minor_locator(MultipleLocator(0.1))\naxes.grid(which=\"major\", axis=\"x\", linewidth=0.75, linestyle=\"-\", color=\"0.75\")\naxes.grid(which=\"minor\", axis=\"x\", linewidth=0.25, linestyle=\"-\", color=\"0.75\")\naxes.grid(which=\"major\", axis=\"y\", linewidth=0.75, linestyle=\"-\", color=\"0.75\")\naxes.grid(which=\"minor\", axis=\"y\", linewidth=0.25, linestyle=\"-\", color=\"0.75\")\naxes.set_xticklabels([])\naxes.set_yticklabels([])\n\n\n# Add a title and a box around it\nfrom matplotlib.patches import FancyBboxPatch\n\nax = plt.gca()\nax.add_patch(\n FancyBboxPatch(\n (-0.05, 0.87),\n width=0.66,\n height=0.165,\n clip_on=False,\n boxstyle=\"square,pad=0\",\n zorder=3,\n facecolor=\"white\",\n alpha=1.0,\n transform=plt.gca().transAxes,\n )\n)\n\nplt.text(\n -0.05,\n 1.02,\n \" Grid: plt.grid(...)\\n\",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"xx-large\",\n transform=axes.transAxes,\n)\n\nplt.text(\n -0.05,\n 1.01,\n \"\\n\\n Draw ticks and grid \",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"large\",\n transform=axes.transAxes,\n)"
]
}
],
"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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Imshow demo\n\nDemoing imshow\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\n\ndef f(x, y):\n return (1 - x / 2 + x**5 + y**3) * np.exp(-(x**2) - y**2)\n\n\nn = 10\nx = np.linspace(-3, 3, 8 * n)\ny = np.linspace(-3, 3, 6 * n)\nX, Y = np.meshgrid(x, y)\nZ = f(X, Y)\nplt.imshow(Z, interpolation=\"nearest\", cmap=\"bone\", origin=\"lower\")\nplt.xticks([])\nplt.yticks([])\n\n\n# Add a title and a box around it\nfrom matplotlib.patches import FancyBboxPatch\n\nax = plt.gca()\nax.add_patch(\n FancyBboxPatch(\n (-0.05, 0.87),\n width=0.66,\n height=0.165,\n clip_on=False,\n boxstyle=\"square,pad=0\",\n zorder=3,\n facecolor=\"white\",\n alpha=1.0,\n transform=plt.gca().transAxes,\n )\n)\n\nplt.text(\n -0.05,\n 1.02,\n \" Imshow: plt.imshow(...)\\n\",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"xx-large\",\n transform=plt.gca().transAxes,\n)\n\nplt.text(\n -0.05,\n 1.01,\n \"\\n\\n Display an image to current axes \",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n family=\"DejaVu Sans\",\n size=\"large\",\n transform=plt.gca().transAxes,\n)\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Multiple plots vignette\n\nDemo multiple plots and style the figure.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nax = plt.subplot(2, 1, 1)\nax.set_xticklabels([])\nax.set_yticklabels([])\n\n\n# Add a title and a box around it\nfrom matplotlib.patches import FancyBboxPatch\n\nax = plt.gca()\nax.add_patch(\n FancyBboxPatch(\n (-0.05, 0.72),\n width=0.66,\n height=0.34,\n clip_on=False,\n boxstyle=\"square,pad=0\",\n zorder=3,\n facecolor=\"white\",\n alpha=1.0,\n transform=plt.gca().transAxes,\n )\n)\n\nplt.text(\n -0.05,\n 1.02,\n \" Multiplot: plt.subplot(...)\\n\",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"xx-large\",\n transform=ax.transAxes,\n)\nplt.text(\n -0.05,\n 1.01,\n \"\\n\\n Plot several plots at once \",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"large\",\n transform=ax.transAxes,\n)\n\nax = plt.subplot(2, 2, 3)\nax.set_xticklabels([])\nax.set_yticklabels([])\n\nax = plt.subplot(2, 2, 4)\nax.set_xticklabels([])\nax.set_yticklabels([])\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Pie chart vignette\n\nDemo pie chart with matplotlib and style the figure.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nn = 20\nX = np.ones(n)\nX[-1] *= 2\nplt.pie(X, explode=X * 0.05, colors=[f\"{i / float(n):f}\" for i in range(n)])\n\nfig = plt.gcf()\nw, h = fig.get_figwidth(), fig.get_figheight()\nr = h / float(w)\n\nplt.xlim(-1.5, 1.5)\nplt.ylim(-1.5 * r, 1.5 * r)\nplt.xticks([])\nplt.yticks([])\n\n\n# Add a title and a box around it\nfrom matplotlib.patches import FancyBboxPatch\n\nax = plt.gca()\nax.add_patch(\n FancyBboxPatch(\n (-0.05, 0.87),\n width=0.66,\n height=0.165,\n clip_on=False,\n boxstyle=\"square,pad=0\",\n zorder=3,\n facecolor=\"white\",\n alpha=1.0,\n transform=plt.gca().transAxes,\n )\n)\n\nplt.text(\n -0.05,\n 1.02,\n \" Pie Chart: plt.pie(...)\\n\",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"xx-large\",\n transform=plt.gca().transAxes,\n)\n\nplt.text(\n -0.05,\n 1.01,\n \"\\n\\n Make a pie chart of an array \",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"large\",\n transform=plt.gca().transAxes,\n)\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# 3D plotting vignette\n\nDemo 3D plotting with matplotlib and decorate the figure.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\n\nx = np.arange(-4, 4, 0.25)\ny = np.arange(-4, 4, 0.25)\nX, Y = np.meshgrid(x, y)\nR = np.sqrt(X**2 + Y**2)\nZ = np.sin(R)\n\nfig = plt.figure()\nax: Axes3D = fig.add_subplot(111, projection=\"3d\")\n\nax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=\"hot\")\nax.contourf(X, Y, Z, zdir=\"z\", offset=-2, cmap=\"hot\")\n\nax.set_zlim(-2, 2)\nplt.xticks([])\nplt.yticks([])\nax.set_zticks([])\n\nax.text2D(\n 0.05,\n 0.93,\n \" 3D plots \\n\",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"xx-large\",\n bbox={\"facecolor\": \"white\", \"alpha\": 1.0},\n transform=plt.gca().transAxes,\n)\n\nax.text2D(\n 0.05,\n 0.87,\n \" Plot 2D or 3D data\",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"large\",\n transform=plt.gca().transAxes,\n)\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Plot example vignette\n\nAn example of plots with matplotlib, and added annotations.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nn = 256\nX = np.linspace(0, 2, n)\nY = np.sin(2 * np.pi * X)\n\nplt.plot(X, Y, lw=2, color=\"violet\")\nplt.xlim(-0.2, 2.2)\nplt.xticks([])\nplt.ylim(-1.2, 1.2)\nplt.yticks([])\n\n\n# Add a title and a box around it\nfrom matplotlib.patches import FancyBboxPatch\n\nax = plt.gca()\nax.add_patch(\n FancyBboxPatch(\n (-0.05, 0.87),\n width=0.66,\n height=0.165,\n clip_on=False,\n boxstyle=\"square,pad=0\",\n zorder=3,\n facecolor=\"white\",\n alpha=1.0,\n transform=plt.gca().transAxes,\n )\n)\n\nplt.text(\n -0.05,\n 1.02,\n \" Regular Plot: plt.plot(...)\\n\",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"xx-large\",\n transform=plt.gca().transAxes,\n)\n\nplt.text(\n -0.05,\n 1.01,\n \"\\n\\n Plot lines and/or markers \",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"large\",\n transform=plt.gca().transAxes,\n)\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Plotting in polar, decorated\n\nAn example showing how to plot in polar coordinate, and some\ndecorations.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n\nimport matplotlib\nimport matplotlib.pyplot as plt\n\n\nplt.subplot(1, 1, 1, polar=True)\n\nN = 20\ntheta = np.arange(0.0, 2 * np.pi, 2 * np.pi / N)\nrng = np.random.default_rng()\nradii = 10 * rng.random(N)\nwidth = np.pi / 4 * rng.random(N)\nbars = plt.bar(theta, radii, width=width, bottom=0.0)\njet = matplotlib.colormaps[\"jet\"]\n\nfor r, bar in zip(radii, bars, strict=True):\n bar.set_facecolor(jet(r / 10.0))\n bar.set_alpha(0.5)\nplt.gca().set_xticklabels([])\nplt.gca().set_yticklabels([])\n\n\nplt.text(\n -0.2,\n 1.02,\n \" Polar Axis \\n\",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"xx-large\",\n bbox={\"facecolor\": \"white\", \"alpha\": 1.0},\n transform=plt.gca().transAxes,\n)\n\nplt.text(\n -0.2,\n 1.01,\n \"\\n\\n Plot anything using polar axis \",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"large\",\n transform=plt.gca().transAxes,\n)\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Plotting quiver decorated\n\nAn example showing quiver with decorations.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nn = 8\nX, Y = np.mgrid[0:n, 0:n]\nT = np.arctan2(Y - n / 2.0, X - n / 2.0)\nR = 10 + np.sqrt((Y - n / 2.0) ** 2 + (X - n / 2.0) ** 2)\nU, V = R * np.cos(T), R * np.sin(T)\n\nplt.quiver(X, Y, U, V, R, alpha=0.5)\nplt.quiver(X, Y, U, V, edgecolor=\"k\", facecolor=\"None\", linewidth=0.5)\n\nplt.xlim(-1, n)\nplt.xticks([])\nplt.ylim(-1, n)\nplt.yticks([])\n\n\n# Add a title and a box around it\nfrom matplotlib.patches import FancyBboxPatch\n\nax = plt.gca()\nax.add_patch(\n FancyBboxPatch(\n (-0.05, 0.87),\n width=0.66,\n height=0.165,\n clip_on=False,\n boxstyle=\"square,pad=0\",\n zorder=3,\n facecolor=\"white\",\n alpha=1.0,\n transform=plt.gca().transAxes,\n )\n)\n\nplt.text(\n -0.05,\n 1.02,\n \" Quiver Plot: plt.quiver(...)\\n\",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"xx-large\",\n transform=plt.gca().transAxes,\n)\n\nplt.text(\n -0.05,\n 1.01,\n \"\\n\\n Plot a 2-D field of arrows \",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"large\",\n transform=plt.gca().transAxes,\n)\n\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Plot scatter decorated\n\nAn example showing the scatter function, with decorations.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nn = 1024\nrng = np.random.default_rng()\nX = rng.normal(0, 1, n)\nY = rng.normal(0, 1, n)\n\nT = np.arctan2(Y, X)\n\nplt.scatter(X, Y, s=75, c=T, alpha=0.5)\nplt.xlim(-1.5, 1.5)\nplt.xticks([])\nplt.ylim(-1.5, 1.5)\nplt.yticks([])\n\n\n# Add a title and a box around it\nfrom matplotlib.patches import FancyBboxPatch\n\nax = plt.gca()\nax.add_patch(\n FancyBboxPatch(\n (-0.05, 0.87),\n width=0.66,\n height=0.165,\n clip_on=False,\n boxstyle=\"square,pad=0\",\n zorder=3,\n facecolor=\"white\",\n alpha=1.0,\n transform=plt.gca().transAxes,\n )\n)\n\nplt.text(\n -0.05,\n 1.02,\n \" Scatter Plot: plt.scatter(...)\\n\",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"xx-large\",\n transform=plt.gca().transAxes,\n)\n\nplt.text(\n -0.05,\n 1.01,\n \"\\n\\n Make a scatter plot of x versus y \",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"large\",\n transform=plt.gca().transAxes,\n)\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Text printing decorated\n\nAn example showing text printing and decorating the resulting figure.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nfig = plt.figure()\nplt.xticks([])\nplt.yticks([])\n\neqs = []\neqs.append(\n r\"$W^{3\\beta}_{\\delta_1 \\rho_1 \\sigma_2} = U^{3\\beta}_{\\delta_1 \\rho_1} + \\frac{1}{8 \\pi 2} \\int^{\\alpha_2}_{\\alpha_2} d \\alpha^\\prime_2 \\left[\\frac{ U^{2\\beta}_{\\delta_1 \\rho_1} - \\alpha^\\prime_2U^{1\\beta}_{\\rho_1 \\sigma_2} }{U^{0\\beta}_{\\rho_1 \\sigma_2}}\\right]$\"\n)\neqs.append(\n r\"$\\frac{d\\rho}{d t} + \\rho \\vec{v}\\cdot\\nabla\\vec{v} = -\\nabla p + \\mu\\nabla^2 \\vec{v} + \\rho \\vec{g}$\"\n)\neqs.append(r\"$\\int_{-\\infty}^\\infty e^{-x^2}dx=\\sqrt{\\pi}$\")\neqs.append(r\"$E = mc^2 = \\sqrt{{m_0}^2c^4 + p^2c^2}$\")\neqs.append(r\"$F_G = G\\frac{m_1m_2}{r^2}$\")\n\nrng = np.random.default_rng()\n\nfor i in range(24):\n index = rng.integers(0, len(eqs))\n eq = eqs[index]\n size = rng.uniform(12, 32)\n x, y = rng.uniform(0, 1, 2)\n alpha = rng.uniform(0.25, 0.75)\n plt.text(\n x,\n y,\n eq,\n ha=\"center\",\n va=\"center\",\n color=\"#11557c\",\n alpha=alpha,\n transform=plt.gca().transAxes,\n fontsize=size,\n clip_on=True,\n )\n\n\n# Add a title and a box around it\nfrom matplotlib.patches import FancyBboxPatch\n\nax = plt.gca()\nax.add_patch(\n FancyBboxPatch(\n (-0.05, 0.87),\n width=0.66,\n height=0.165,\n clip_on=False,\n boxstyle=\"square,pad=0\",\n zorder=3,\n facecolor=\"white\",\n alpha=1.0,\n transform=plt.gca().transAxes,\n )\n)\n\nplt.text(\n -0.05,\n 1.02,\n \" Text: plt.text(...)\\n\",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"xx-large\",\n transform=plt.gca().transAxes,\n)\n\nplt.text(\n -0.05,\n 1.01,\n \"\\n\\n Draw any kind of text \",\n horizontalalignment=\"left\",\n verticalalignment=\"top\",\n size=\"large\",\n transform=plt.gca().transAxes,\n)\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,97 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Optimization of a two-parameter function\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n\n\n# Define the function that we are interested in\ndef sixhump(x):\n return (\n (4 - 2.1 * x[0] ** 2 + x[0] ** 4 / 3) * x[0] ** 2\n + x[0] * x[1]\n + (-4 + 4 * x[1] ** 2) * x[1] ** 2\n )\n\n\n# Make a grid to evaluate the function (for plotting)\nxlim = [-2, 2]\nylim = [-1, 1]\nx = np.linspace(*xlim) # type: ignore[call-overload]\ny = np.linspace(*ylim) # type: ignore[call-overload]\nxg, yg = np.meshgrid(x, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A 2D image plot of the function\n Simple visualization in 2D\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nplt.figure()\nplt.imshow(sixhump([xg, yg]), extent=xlim + ylim, origin=\"lower\") # type: ignore[arg-type]\nplt.colorbar()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A 3D surface plot of the function\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from mpl_toolkits.mplot3d import Axes3D\n\nfig = plt.figure()\nax: Axes3D = fig.add_subplot(111, projection=\"3d\")\nsurf = ax.plot_surface(\n xg,\n yg,\n sixhump([xg, yg]),\n rstride=1,\n cstride=1,\n cmap=\"viridis\",\n linewidth=0,\n antialiased=False,\n)\n\nax.set_xlabel(\"x\")\nax.set_ylabel(\"y\")\nax.set_zlabel(\"f(x, y)\")\nax.set_title(\"Six-hump Camelback function\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Find minima\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import scipy as sp\n\n# local minimization\nres_local = sp.optimize.minimize(sixhump, x0=[0, 0])\n\n# global minimization\nres_global = sp.optimize.differential_evolution(sixhump, bounds=[xlim, ylim])\n\nplt.figure()\n# Show the function in 2D\nplt.imshow(sixhump([xg, yg]), extent=xlim + ylim, origin=\"lower\") # type: ignore[arg-type]\nplt.colorbar()\n# Mark the minima\nplt.scatter(res_local.x[0], res_local.x[1], label=\"local minimizer\")\nplt.scatter(res_global.x[0], res_global.x[1], label=\"global minimizer\")\nplt.legend()\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,97 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Demo connected components\n\nExtracting and labeling connected components in a 2D array\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Generate some binary data\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x, y = np.indices((100, 100))\nsig = (\n np.sin(2 * np.pi * x / 50.0)\n * np.sin(2 * np.pi * y / 50.0)\n * (1 + x * y / 50.0**2) ** 2\n)\nmask = sig > 1\n\nplt.figure(figsize=(7, 3.5))\nplt.subplot(1, 2, 1)\nplt.imshow(sig)\nplt.axis(\"off\")\nplt.title(\"sig\")\n\nplt.subplot(1, 2, 2)\nplt.imshow(mask, cmap=\"gray\")\nplt.axis(\"off\")\nplt.title(\"mask\")\nplt.subplots_adjust(wspace=0.05, left=0.01, bottom=0.01, right=0.99, top=0.9)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Label connected components\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import scipy as sp\n\nlabels, nb = sp.ndimage.label(mask)\n\nplt.figure(figsize=(3.5, 3.5))\nplt.imshow(labels)\nplt.title(\"label\")\nplt.axis(\"off\")\n\nplt.subplots_adjust(wspace=0.05, left=0.01, bottom=0.01, right=0.99, top=0.9)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Extract the 4th connected component, and crop the array around it\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sl = sp.ndimage.find_objects(labels == 4)\nplt.figure(figsize=(3.5, 3.5))\nplt.imshow(sig[sl[0]])\nplt.title(\"Cropped connected component\")\nplt.axis(\"off\")\n\nplt.subplots_adjust(wspace=0.05, left=0.01, bottom=0.01, right=0.99, top=0.9)\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,86 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Curve fitting\n\nDemos a simple curve fitting\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First generate some data\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n\n# Seed the random number generator for reproducibility\nrng = np.random.default_rng(27446968)\n\nx_data = np.linspace(-5, 5, num=50)\nnoise = 0.01 * np.cos(100 * x_data)\na, b = 2.9, 1.5\ny_data = a * np.cos(b * x_data) + noise\n\n# And plot it\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(6, 4))\nplt.scatter(x_data, y_data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now fit a simple sine function to the data\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import scipy as sp\n\n\ndef test_func(x, a, b, c):\n return a * np.sin(b * x + c)\n\n\nparams, params_covariance = sp.optimize.curve_fit(\n test_func, x_data, y_data, p0=[2, 1, 3]\n)\n\nprint(params)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And plot the resulting curve on the data\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.figure(figsize=(6, 4))\nplt.scatter(x_data, y_data, label=\"Data\")\nplt.plot(x_data, test_func(x_data, *params), label=\"Fitted function\")\n\nplt.legend(loc=\"best\")\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,86 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Detrending a signal\n\n:func:`scipy.signal.detrend` removes a linear trend.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Generate a random signal with a trend\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n\nt = np.linspace(0, 5, 100)\nrng = np.random.default_rng()\nx = t + rng.normal(size=100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Detrend\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import scipy as sp\n\nx_detrended = sp.signal.detrend(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(5, 4))\nplt.plot(t, x, label=\"x\")\nplt.plot(t, x_detrended, label=\"x_detrended\")\nplt.legend(loc=\"best\")\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Normal distribution: histogram and PDF\n\nExplore the normal distribution: a histogram built from samples and the\nPDF (probability density function).\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport scipy as sp\nimport matplotlib.pyplot as plt\n\ndist = sp.stats.norm(loc=0, scale=1) # standard normal distribution\nsample = dist.rvs(size=100000) # \"random variate sample\"\nplt.hist(\n sample,\n bins=51, # group the observations into 50 bins\n density=True, # normalize the frequencies\n label=\"normalized histogram\",\n)\n\nx = np.linspace(-5, 5) # possible values of the random variable\nplt.plot(x, dist.pdf(x), label=\"PDF\")\nplt.legend()\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,86 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Resample a signal with scipy.signal.resample\n\n:func:`scipy.signal.resample` uses FFT to resample a 1D signal.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Generate a signal with 100 data point\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n\nt = np.linspace(0, 5, 100)\nx = np.sin(t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Downsample it by a factor of 4\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import scipy as sp\n\nx_resampled = sp.signal.resample(x, 25)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(5, 4))\nplt.plot(t, x, label=\"Original signal\")\nplt.plot(t[::4], x_resampled, \"ko\", label=\"Resampled signal\")\n\nplt.legend(loc=\"best\")\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,86 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Curve fitting: temperature as a function of month of the year\n\nWe have the min and max temperatures in Alaska for each months of the\nyear. We would like to find a function to describe this yearly evolution.\n\nFor this, we will fit a periodic function.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The data\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n\ntemp_max = np.array([17, 19, 21, 28, 33, 38, 37, 37, 31, 23, 19, 18])\ntemp_min = np.array([-62, -59, -56, -46, -32, -18, -9, -13, -25, -46, -52, -58])\n\nimport matplotlib.pyplot as plt\n\nmonths = np.arange(12)\nplt.plot(months, temp_max, \"ro\")\nplt.plot(months, temp_min, \"bo\")\nplt.xlabel(\"Month\")\nplt.ylabel(\"Min and max temperature\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fitting it to a periodic function\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import scipy as sp\n\n\ndef yearly_temps(times, avg, ampl, time_offset):\n return avg + ampl * np.cos((times + time_offset) * 2 * np.pi / times.max())\n\n\nres_max, cov_max = sp.optimize.curve_fit(yearly_temps, months, temp_max, [20, 10, 0])\nres_min, cov_min = sp.optimize.curve_fit(yearly_temps, months, temp_min, [-40, 20, 0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plotting the fit\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"days = np.linspace(0, 12, num=365)\n\nplt.figure()\nplt.plot(months, temp_max, \"ro\")\nplt.plot(days, yearly_temps(days, *res_max), \"r-\")\nplt.plot(months, temp_min, \"bo\")\nplt.plot(days, yearly_temps(days, *res_min), \"b-\")\nplt.xlabel(\"Month\")\nplt.ylabel(r\"Temperature ($^\\circ$C)\")\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,122 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Image denoising by FFT\n\nDenoise an image (:download:`../../../../data/moonlanding.png`) by\nimplementing a blur with an FFT.\n\nImplements, via FFT, the following convolution:\n\n\\begin{align}f_1(t) = \\int dt'\\, K(t-t') f_0(t')\\end{align}\n\n\\begin{align}\\tilde{f}_1(\\omega) = \\tilde{K}(\\omega) \\tilde{f}_0(\\omega)\\end{align}\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Read and plot the image\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\n\nim = plt.imread(\"../../../../data/moonlanding.png\").astype(float)\n\nplt.figure()\nplt.imshow(im, \"gray\")\nplt.title(\"Original image\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compute the 2d FFT of the input image\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import scipy as sp\n\nim_fft = sp.fft.fft2(im)\n\n# Show the results\n\n\ndef plot_spectrum(im_fft):\n from matplotlib.colors import LogNorm\n\n # A logarithmic colormap\n plt.imshow(np.abs(im_fft), norm=LogNorm(vmin=5))\n plt.colorbar()\n\n\nplt.figure()\nplot_spectrum(im_fft)\nplt.title(\"Fourier transform\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Filter in FFT\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# In the lines following, we'll make a copy of the original spectrum and\n# truncate coefficients.\n\n# Define the fraction of coefficients (in each direction) we keep\nkeep_fraction = 0.1\n\n# Call ff a copy of the original transform. NumPy arrays have a copy\n# method for this purpose.\nim_fft2 = im_fft.copy()\n\n# Set r and c to be the number of rows and columns of the array.\nr, c = im_fft2.shape\n\n# Set to zero all rows with indices between r*keep_fraction and\n# r*(1-keep_fraction):\nim_fft2[int(r * keep_fraction) : int(r * (1 - keep_fraction))] = 0\n\n# Similarly with the columns:\nim_fft2[:, int(c * keep_fraction) : int(c * (1 - keep_fraction))] = 0\n\nplt.figure()\nplot_spectrum(im_fft2)\nplt.title(\"Filtered Spectrum\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reconstruct the final image\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Reconstruct the denoised image from the filtered spectrum, keep only the\n# real part for display.\nim_new = sp.fft.ifft2(im_fft2).real\n\nplt.figure()\nplt.imshow(im_new, \"gray\")\nplt.title(\"Reconstructed Image\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Easier and better: :func:`scipy.ndimage.gaussian_filter`\n\n Implementing filtering directly with FFTs is tricky and time consuming.\n We can use the Gaussian filter from :mod:`scipy.ndimage`\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"im_blur = sp.ndimage.gaussian_filter(im, 4)\n\nplt.figure()\nplt.imshow(im_blur, \"gray\")\nplt.title(\"Blurred image\")\n\nplt.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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,140 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Simple image blur by convolution with a Gaussian kernel\n\nBlur an an image (:download:`../../../../data/elephant.png`) using a\nGaussian kernel.\n\nConvolution is easy to perform with FFT: convolving two signals boils\ndown to multiplying their FFTs (and performing an inverse FFT).\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport scipy as sp\nimport matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The original image\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# read image\nimg = plt.imread(\"../../../../data/elephant.png\")\nplt.figure()\nplt.imshow(img)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prepare an Gaussian convolution kernel\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# First a 1-D Gaussian\nt = np.linspace(-10, 10, 30)\nbump = np.exp(-0.1 * t**2)\nbump /= np.trapezoid(bump) # normalize the integral to 1\n\n# make a 2-D kernel out of it\nkernel = bump[:, np.newaxis] * bump[np.newaxis, :]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Implement convolution via FFT\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Padded fourier transform, with the same shape as the image\n# We use :func:`scipy.fft.fft2` to have a 2D FFT\nkernel_ft = sp.fft.fft2(kernel, s=img.shape[:2], axes=(0, 1))\n\n# convolve\nimg_ft = sp.fft.fft2(img, axes=(0, 1))\n# the 'newaxis' is to match to color direction\nimg2_ft = kernel_ft[:, :, np.newaxis] * img_ft\nimg2 = sp.fft.ifft2(img2_ft, axes=(0, 1)).real\n\n# clip values to range\nimg2 = np.clip(img2, 0, 1)\n\n# plot output\nplt.figure()\nplt.imshow(img2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Further exercise (only if you are familiar with this stuff):\n\nA \"wrapped border\" appears in the upper left and top edges of the\nimage. This is because the padding is not done correctly, and does\nnot take the kernel size into account (so the convolution \"flows out\nof bounds of the image\"). Try to remove this artifact.\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A function to do it: :func:`scipy.signal.fftconvolve`\n\n The above exercise was only for didactic reasons: there exists a\n function in scipy that will do this for us, and probably do a better\n job: :func:`scipy.signal.fftconvolve`\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# mode='same' is there to enforce the same output shape as input arrays\n# (ie avoid border effects)\nimg3 = sp.signal.fftconvolve(img, kernel[:, :, np.newaxis], mode=\"same\")\nplt.figure()\nplt.imshow(img3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that we still have a decay to zero at the border of the image.\nUsing :func:`scipy.ndimage.gaussian_filter` would get rid of this\nartifact\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,93 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Crude periodicity finding\n\nDiscover the periods in evolution of animal populations\n(:download:`../../../../data/populations.txt`)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load the data\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n\ndata = np.loadtxt(\"../../../../data/populations.txt\")\nyears = data[:, 0]\npopulations = data[:, 1:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plot the data\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nplt.figure()\nplt.plot(years, populations * 1e-3)\nplt.xlabel(\"Year\")\nplt.ylabel(r\"Population number ($\\cdot10^3$)\")\nplt.legend([\"hare\", \"lynx\", \"carrot\"], loc=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plot its periods\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import scipy as sp\n\nft_populations = sp.fft.fft(populations, axis=0)\nfrequencies = sp.fft.fftfreq(populations.shape[0], years[1] - years[0])\nperiods = 1 / frequencies\n\nplt.figure()\nplt.plot(periods, abs(ft_populations) * 1e-3, \"o\")\nplt.xlim(0, 22)\nplt.xlabel(\"Period\")\nplt.ylabel(r\"Power ($\\cdot10^3$)\")\n\nplt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There's probably a period of around 10 years (obvious from the\nplot), but for this crude a method, there's not enough data to say\nmuch more.\n\n"
]
}
],
"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",
"pygments_lexer": "ipython3",
"version": "3.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,18 @@
# Scientific Python Lectures 速览包
> 2025-10-19 留存 | 来源:[lectures.scientific-python.org](https://lectures.scientific-python.org/index.html)
## 📌 是什么
官方出品的一套 **“一站式”科学 Python 教程合集**,每章 ≈12 小时,难度从入门到专家递进,配套代码可直接跑。
## 📂 目录快照
| 模块 | 亮点 |
|---|---|
| **1. Getting started** | Python 生态、NumPy、Matplotlib、SciPy、查文档技巧 |
| **2. Advanced topics** | 高级语法、调试、性能优化、稀疏矩阵、图像处理、数学优化、C 语言接口 |
| **3. Packages & apps** | 统计学、SymPy 符号计算、scikit-image、scikit-learn |
## 🚀 怎么用
1. 进入首页 → 右上角 **“Download PDF”** 可离线阅读。
2. 每章底部有 **Jupyter Notebook 下载链接**,本地一键运行。
3. 源码仓库:[`github.com/scientific-python/scientific-python-lectures`](https://github.com/scientific-python/scientific-python-lectures)