Skip to content

Commit

Permalink
adding tests from nb
Browse files Browse the repository at this point in the history
Signed-off-by: Ross Turk <[email protected]>
  • Loading branch information
rossturk committed Nov 20, 2024
1 parent 2814460 commit 7416e1d
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 1 deletion.
2 changes: 1 addition & 1 deletion jupyterlab/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.ipynb_checkpoints
/*.nbconvert.ipynb
7 changes: 7 additions & 0 deletions jupyterlab/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Jupyter Notebooks + Flox

Blog: https://flox.dev/blog/jupyter-remote-env

Examples:
- https://matplotlib.org/3.1.3/gallery/images_contours_and_fields/barcode_demo.html
- https://matplotlib.org/stable/gallery/images_contours_and_fields/tricontour_demo.html
72 changes: 72 additions & 0 deletions jupyterlab/barcode_demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Barcode Demo\n\n\nThis demo shows how to produce a one-dimensional image, or \"bar code\".\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\nimport numpy as np\n\n# Fixing random state for reproducibility\nnp.random.seed(19680801)\n\n# the bar\nx = np.random.rand(500) > 0.7\n\nbarprops = dict(aspect='auto', cmap='binary', interpolation='nearest')\n\nfig = plt.figure()\n\n# a vertical barcode\nax1 = fig.add_axes([0.1, 0.1, 0.1, 0.8])\nax1.set_axis_off()\nax1.imshow(x.reshape((-1, 1)), **barprops)\n\n# a horizontal barcode\nax2 = fig.add_axes([0.3, 0.4, 0.6, 0.2])\nax2.set_axis_off()\nax2.imshow(x.reshape((1, -1)), **barprops)\n\nplt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"------------\n\nReferences\n\"\"\"\"\"\"\"\"\"\"\n\nThe use of the following functions, methods and classes is shown\nin this example:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib\nmatplotlib.axes.Axes.imshow\nmatplotlib.pyplot.imshow"
]
}
],
"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.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
7 changes: 7 additions & 0 deletions jupyterlab/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -eo pipefail

jupyter nbconvert --to notebook --execute ./barcode_demo.ipynb
jupyter nbconvert --to notebook --execute ./tricontour_demo.ipynb

158 changes: 158 additions & 0 deletions jupyterlab/tricontour_demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Tricontour Demo\n\nContour plots of unstructured triangular grids.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.tri as tri"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Creating a Triangulation without specifying the triangles results in the\nDelaunay triangulation of the points.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# First create the x and y coordinates of the points.\nn_angles = 48\nn_radii = 8\nmin_radius = 0.25\nradii = np.linspace(min_radius, 0.95, n_radii)\n\nangles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)\nangles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)\nangles[:, 1::2] += np.pi / n_angles\n\nx = (radii * np.cos(angles)).flatten()\ny = (radii * np.sin(angles)).flatten()\nz = (np.cos(radii) * np.cos(3 * angles)).flatten()\n\n# Create the Triangulation; no triangles so Delaunay triangulation created.\ntriang = tri.Triangulation(x, y)\n\n# Mask off unwanted triangles.\ntriang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),\n y[triang.triangles].mean(axis=1))\n < min_radius)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pcolor plot.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"fig1, ax1 = plt.subplots()\nax1.set_aspect('equal')\ntcf = ax1.tricontourf(triang, z)\nfig1.colorbar(tcf)\nax1.tricontour(triang, z, colors='k')\nax1.set_title('Contour plot of Delaunay triangulation')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You could also specify hatching patterns along with different cmaps.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"fig2, ax2 = plt.subplots()\nax2.set_aspect(\"equal\")\ntcf = ax2.tricontourf(\n triang,\n z,\n hatches=[\"*\", \"-\", \"/\", \"//\", \"\\\\\", None],\n cmap=\"cividis\"\n)\nfig2.colorbar(tcf)\nax2.tricontour(triang, z, linestyles=\"solid\", colors=\"k\", linewidths=2.0)\nax2.set_title(\"Hatched Contour plot of Delaunay triangulation\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You could also generate hatching patterns labeled with no color.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"fig3, ax3 = plt.subplots()\nn_levels = 7\ntcf = ax3.tricontourf(\n triang,\n z,\n n_levels,\n colors=\"none\",\n hatches=[\".\", \"/\", \"\\\\\", None, \"\\\\\\\\\", \"*\"],\n)\nax3.tricontour(triang, z, n_levels, colors=\"black\", linestyles=\"-\")\n\n\n# create a legend for the contour set\nartists, labels = tcf.legend_elements(str_format=\"{:2.1f}\".format)\nax3.legend(artists, labels, handleheight=2, framealpha=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can specify your own triangulation rather than perform a Delaunay\ntriangulation of the points, where each triangle is given by the indices of\nthe three points that make up the triangle, ordered in either a clockwise or\nanticlockwise manner.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"xy = np.asarray([\n [-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890],\n [-0.045, 0.897], [-0.057, 0.895], [-0.073, 0.900], [-0.087, 0.898],\n [-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919],\n [-0.073, 0.928], [-0.052, 0.930], [-0.048, 0.942], [-0.062, 0.949],\n [-0.054, 0.958], [-0.069, 0.954], [-0.087, 0.952], [-0.087, 0.959],\n [-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965],\n [-0.097, 0.975], [-0.092, 0.984], [-0.101, 0.980], [-0.108, 0.980],\n [-0.104, 0.987], [-0.102, 0.993], [-0.115, 1.001], [-0.099, 0.996],\n [-0.101, 1.007], [-0.090, 1.010], [-0.087, 1.021], [-0.069, 1.021],\n [-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005],\n [-0.048, 1.005], [-0.031, 1.005], [-0.031, 0.996], [-0.040, 0.987],\n [-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968],\n [-0.020, 0.954], [-0.006, 0.947], [ 0.003, 0.935], [ 0.006, 0.926],\n [ 0.005, 0.921], [ 0.022, 0.923], [ 0.033, 0.912], [ 0.029, 0.905],\n [ 0.017, 0.900], [ 0.012, 0.895], [ 0.027, 0.893], [ 0.019, 0.886],\n [ 0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879],\n [-0.057, 0.881], [-0.062, 0.876], [-0.078, 0.876], [-0.087, 0.872],\n [-0.030, 0.907], [-0.007, 0.905], [-0.057, 0.916], [-0.025, 0.933],\n [-0.077, 0.990], [-0.059, 0.993]])\nx = np.degrees(xy[:, 0])\ny = np.degrees(xy[:, 1])\nx0 = -5\ny0 = 52\nz = np.exp(-0.01 * ((x - x0) ** 2 + (y - y0) ** 2))\n\ntriangles = np.asarray([\n [67, 66, 1], [65, 2, 66], [ 1, 66, 2], [64, 2, 65], [63, 3, 64],\n [60, 59, 57], [ 2, 64, 3], [ 3, 63, 4], [ 0, 67, 1], [62, 4, 63],\n [57, 59, 56], [59, 58, 56], [61, 60, 69], [57, 69, 60], [ 4, 62, 68],\n [ 6, 5, 9], [61, 68, 62], [69, 68, 61], [ 9, 5, 70], [ 6, 8, 7],\n [ 4, 70, 5], [ 8, 6, 9], [56, 69, 57], [69, 56, 52], [70, 10, 9],\n [54, 53, 55], [56, 55, 53], [68, 70, 4], [52, 56, 53], [11, 10, 12],\n [69, 71, 68], [68, 13, 70], [10, 70, 13], [51, 50, 52], [13, 68, 71],\n [52, 71, 69], [12, 10, 13], [71, 52, 50], [71, 14, 13], [50, 49, 71],\n [49, 48, 71], [14, 16, 15], [14, 71, 48], [17, 19, 18], [17, 20, 19],\n [48, 16, 14], [48, 47, 16], [47, 46, 16], [16, 46, 45], [23, 22, 24],\n [21, 24, 22], [17, 16, 45], [20, 17, 45], [21, 25, 24], [27, 26, 28],\n [20, 72, 21], [25, 21, 72], [45, 72, 20], [25, 28, 26], [44, 73, 45],\n [72, 45, 73], [28, 25, 29], [29, 25, 31], [43, 73, 44], [73, 43, 40],\n [72, 73, 39], [72, 31, 25], [42, 40, 43], [31, 30, 29], [39, 73, 40],\n [42, 41, 40], [72, 33, 31], [32, 31, 33], [39, 38, 72], [33, 72, 38],\n [33, 38, 34], [37, 35, 38], [34, 38, 35], [35, 37, 36]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Rather than create a Triangulation object, can simply pass x, y and triangles\narrays to tripcolor directly. It would be better to use a Triangulation\nobject if the same triangulation was to be used more than once to save\nduplicated calculations.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"fig4, ax4 = plt.subplots()\nax4.set_aspect('equal')\ntcf = ax4.tricontourf(x, y, triangles, z)\nfig4.colorbar(tcf)\nax4.set_title('Contour plot of user-specified triangulation')\nax4.set_xlabel('Longitude (degrees)')\nax4.set_ylabel('Latitude (degrees)')\n\nplt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.tricontourf` / `matplotlib.pyplot.tricontourf`\n - `matplotlib.tri.Triangulation`\n - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`\n - `matplotlib.axes.Axes.legend` / `matplotlib.pyplot.legend`\n - `matplotlib.contour.ContourSet.legend_elements`\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.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit 7416e1d

Please sign in to comment.