{ "cells": [ { "cell_type": "markdown", "id": "63cc4e07-414f-405e-bbae-84440ee6267f", "metadata": {}, "source": [ "(howto-visualization)=\n", "# Visualization" ] }, { "cell_type": "markdown", "id": "e37ffb69-f376-4389-a5f5-10155e8d7b6f", "metadata": {}, "source": [ "(howto-visualization-cyclic-colormaps)=\n", "## Using cyclic colormaps" ] }, { "cell_type": "code", "execution_count": null, "id": "46088a17-5798-4a02-84a7-759e032288e7", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import xarray as xr\n", "\n", "from quantify_core.visualization.mpl_plotting import set_cyclic_colormap\n", "\n", "zvals = xr.DataArray(np.random.rand(6, 10) * 360)\n", "zvals.attrs[\"units\"] = \"deg\"\n", "zvals.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "462363f7-bcee-45d7-af09-ca5604df7596", "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(1, 1)\n", "color_plot = zvals.plot(ax=ax)\n", "set_cyclic_colormap(color_plot)" ] }, { "cell_type": "code", "execution_count": null, "id": "c132f43f-d5d5-453b-a9cf-f6d83232f911", "metadata": {}, "outputs": [], "source": [ "zvals_shifted = zvals - 180\n", "\n", "fig, ax = plt.subplots(1, 1)\n", "color_plot = zvals_shifted.plot(ax=ax)\n", "ax.set_title(\"Shifted cyclic colormap\")\n", "set_cyclic_colormap(color_plot, shifted=zvals_shifted.min() < 0)" ] }, { "cell_type": "code", "execution_count": null, "id": "2ad6a50f-495a-46a4-b105-ee20b64b81e9", "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(1, 1)\n", "color_plot = (zvals / 2).plot(ax=ax)\n", "ax.set_title(\"Overwrite clim\")\n", "set_cyclic_colormap(color_plot, clim=(0, 180), unit=\"deg\")" ] }, { "cell_type": "code", "execution_count": null, "id": "94f0cb57-081e-4ccf-9fea-427406af67d2", "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(1, 1)\n", "zvals_rad = zvals / 180 * np.pi\n", "zvals_rad.attrs[\"units\"] = \"rad\"\n", "color_plot = zvals_rad.plot(ax=ax)\n", "ax.set_title(\"Radians\")\n", "set_cyclic_colormap(color_plot, unit=zvals_rad.units)" ] }, { "cell_type": "markdown", "id": "2e579a6e-5755-4c00-aa5a-e1a3b5e5adee", "metadata": {}, "source": [ "(howto-visualization-custom-colormaps)=\n", "## Creating custom colormaps\n", "\n", "In this example we use this function to create a custom colormap using several\n", "base colors for which we adjust the saturation and transparency (alpha,\n", "only visible when exporting the image)." ] }, { "cell_type": "code", "execution_count": null, "id": "5ad4cd9c-85e4-4474-ac71-a16dd9e0771a", "metadata": {}, "outputs": [], "source": [ "import colorsys\n", "\n", "import matplotlib.colors as mplc\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "from quantify_core.visualization.color_utilities import set_hlsa\n", "\n", "color_cycle = [\"#1f77b4\", \"#ff7f0e\", \"#2ca02c\", \"#d62728\"]\n", "all_colors = []\n", "for col in color_cycle:\n", " hls = colorsys.rgb_to_hls(*mplc.to_rgb(mplc.to_rgb(col)))\n", " sat_vals = (np.linspace(0.0, 1.0, 20) ** 2) * hls[2]\n", " alpha_vals = np.linspace(0.4, 1.0, 20)\n", "\n", " colors = [\n", " list(set_hlsa(col, s=s)) for s, a in zip(sat_vals, alpha_vals)\n", " ]\n", " all_colors += colors\n", "\n", "cmap = mplc.ListedColormap(all_colors)\n", "\n", "np.random.seed(19680801)\n", "data = np.random.randn(30, 30)\n", "\n", "fig, ax = plt.subplots(1, 1, figsize=(6, 3), constrained_layout=True)\n", "\n", "psm = ax.pcolormesh(data, cmap=cmap, rasterized=True, vmin=-4, vmax=4)\n", "fig.colorbar(psm, ax=ax)\n", "plt.show()" ] } ], "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.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }