{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chose the colormap interactively" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from pathlib import Path\n", "\n", "options = sorted(Path(\"data\").glob(\"*/*.pickle\"))\n", "\n", "cmaps = [\n", " (\n", " \"Perceptually Uniform Sequential\",\n", " [\"viridis\", \"plasma\", \"inferno\", \"magma\", \"cividis\"],\n", " ),\n", " (\n", " \"Sequential\",\n", " [\n", " \"Greys\",\n", " \"Purples\",\n", " \"Blues\",\n", " \"Greens\",\n", " \"Oranges\",\n", " \"Reds\",\n", " \"YlOrBr\",\n", " \"YlOrRd\",\n", " \"OrRd\",\n", " \"PuRd\",\n", " \"RdPu\",\n", " \"BuPu\",\n", " \"GnBu\",\n", " \"PuBu\",\n", " \"YlGnBu\",\n", " \"PuBuGn\",\n", " \"BuGn\",\n", " \"YlGn\",\n", " ],\n", " ),\n", " (\n", " \"Sequential (2)\",\n", " [\n", " \"binary\",\n", " \"gist_yarg\",\n", " \"gist_gray\",\n", " \"gray\",\n", " \"bone\",\n", " \"pink\",\n", " \"spring\",\n", " \"summer\",\n", " \"autumn\",\n", " \"winter\",\n", " \"cool\",\n", " \"Wistia\",\n", " \"hot\",\n", " \"afmhot\",\n", " \"gist_heat\",\n", " \"copper\",\n", " ],\n", " ),\n", " (\n", " \"Diverging\",\n", " [\n", " \"PiYG\",\n", " \"PRGn\",\n", " \"BrBG\",\n", " \"PuOr\",\n", " \"RdGy\",\n", " \"RdBu\",\n", " \"RdYlBu\",\n", " \"RdYlGn\",\n", " \"Spectral\",\n", " \"coolwarm\",\n", " \"bwr\",\n", " \"seismic\",\n", " ],\n", " ),\n", " (\"Cyclic\", [\"twilight\", \"twilight_shifted\", \"hsv\"]),\n", " (\n", " \"Qualitative\",\n", " [\n", " \"Pastel1\",\n", " \"Pastel2\",\n", " \"Paired\",\n", " \"Accent\",\n", " \"Dark2\",\n", " \"Set1\",\n", " \"Set2\",\n", " \"Set3\",\n", " \"tab10\",\n", " \"tab20\",\n", " \"tab20b\",\n", " \"tab20c\",\n", " ],\n", " ),\n", " (\n", " \"Miscellaneous\",\n", " [\n", " \"flag\",\n", " \"prism\",\n", " \"ocean\",\n", " \"gist_earth\",\n", " \"terrain\",\n", " \"gist_stern\",\n", " \"gnuplot\",\n", " \"gnuplot2\",\n", " \"CMRmap\",\n", " \"cubehelix\",\n", " \"brg\",\n", " \"gist_rainbow\",\n", " \"rainbow\",\n", " \"jet\",\n", " \"nipy_spectral\",\n", " \"gist_ncar\",\n", " ],\n", " ),\n", "]\n", "\n", "all_cmaps = [f\"{k}: {cm}\" for k, _cmaps in cmaps for cm in _cmaps]\n", "\n", "\n", "def plot_color_gradients(cmap_category, cmap_list):\n", " # Create figure and adjust figure height to number of colormaps\n", "\n", " nrows = len(cmap_list)\n", " figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22\n", " fig, axes = plt.subplots(nrows=nrows, figsize=(6.4, figh))\n", " fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh, left=0.2, right=0.99)\n", "\n", " axes[0].set_title(cmap_category + \" colormaps\", fontsize=14)\n", "\n", " gradient = np.linspace(0, 1, 256)\n", " gradient = np.vstack((gradient, gradient))\n", " for ax, name in zip(axes, cmap_list):\n", " ax.imshow(gradient, aspect=\"auto\", cmap=plt.get_cmap(name))\n", " ax.text(\n", " -0.01,\n", " 0.5,\n", " name,\n", " va=\"center\",\n", " ha=\"right\",\n", " fontsize=10,\n", " transform=ax.transAxes,\n", " )\n", "\n", " # Turn off *all* ticks & spines, not just the ones with colormaps.\n", " for ax in axes:\n", " ax.set_axis_off()\n", "\n", "\n", "for cmap_category, cmap_list in cmaps:\n", " plot_color_gradients(cmap_category, cmap_list)\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import adaptive\n", "import matplotlib.colors as colors\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from ipywidgets import FloatSlider, IntSlider, interact\n", "\n", "from thesis_cover import load_learner, get_cmap\n", "\n", "x_size, y_size = xy_size = (14.13, 9.84)\n", "\n", "\n", "\n", "def plot(fname, cmap, min_clip=0.0, max_clip=1.0, exp=1.0, invert=False):\n", " learner = load_learner(fname)\n", " xs, ys, zs = data = learner.interpolated_on_grid(1000)\n", " cmap = cmap.split(\": \")[-1] + (\"_r\" if invert else \"\")\n", "\n", " zs = get_cmap(cmap, min_clip, max_clip, exp)(zs)\n", " fig, ax = plt.subplots(figsize=xy_size)\n", " im = ax.imshow(\n", " np.rot90(zs),\n", " extent=(-0.5 * x_size, 0.5 * x_size, -0.5 * y_size, 0.5 * y_size),\n", " zorder=3,\n", " )\n", " plt.show()\n", "\n", "\n", "interact(\n", " plot,\n", " fname=options,\n", " cmap=all_cmaps,\n", " min_clip=FloatSlider(min=0, max=1, step=0.05),\n", " max_clip=FloatSlider(value=1, min=0, max=1, step=0.05),\n", " exp=FloatSlider(value=1, min=0.1, max=3, step=0.05),\n", ")" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 4 }