{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# 2D Mixed-Histogram\n\nData on a regular two-dimensional grid which is the combination\nof an image and a histogram.\n\nThe x-axis defines the bin edges along the second dimension.\nThe y-axis defines the grid coordinates along the first dimension.\n\n.. code::\n\n @NX_class = \"NXroot\"\n @default = \"scan1\"\n scan1:\n @NX_class = \"NXentry\"\n @default = \"data\"\n data:\n @NX_class = \"NXdata\"\n @axes = [\"y\", \"x\"]\n @signal = \"z\"\n x: NX_FLOAT64[7]\n y: NX_FLOAT64[16]\n z: NX_FLOAT64[16,6]\n\nExplanation:\n\n1. ``@axes`` has two values which corresponds to the signal rank of two.\n\n2. ``z`` is the default signal to be plotted versus ``x`` and ``y``.\n\n3. ``z`` has 16 rows and 6 columns.\n\n4. ``x`` has one more value than the second dimension of ``z`` since it contains the bin edges.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Data\nimport numpy as np\n\nx = [-3.0, -2.5, -1.0, 0.0, 1.0, 2.5, 3.0]\ny = np.linspace(-3, 3, 16)\n\nnx = len(x) - 1\nny = len(y)\nz = np.zeros((ny, nx))\nxx = np.linspace(-3, 3, 200)\nfor i in range(ny):\n zi = (1 - xx / 2 + xx**5 + y[i] ** 3) * np.exp(-(xx**2) - y[i] ** 2)\n z[i, :], _ = np.histogram(xx, bins=x, weights=zi)\n\n# Plot\nimport matplotlib.pyplot as plt # noqa E402\n\nplt.style.use(\"_mpl-gallery-nogrid\")\n\nfig, ax = plt.subplots()\n\ny_new = np.empty_like(y, shape=(len(y) + 1,))\ny_new[0] = 2 * y[0] - y[1]\ny_new[1:-1] = (y[:-1] + y[1:]) / 2\ny_new[-1] = 2 * y[-1] - y[-2]\n\nmesh = ax.pcolormesh(x, y_new, z, linewidth=0.7)\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.8.16" } }, "nbformat": 4, "nbformat_minor": 0 }