Chose the colormap interactively

In [ ]:
import matplotlib.pyplot as plt
import numpy as np
from pathlib import Path

options = sorted(Path("data").glob("*/*.pickle"))

cmaps = [
    (
        "Perceptually Uniform Sequential",
        ["viridis", "plasma", "inferno", "magma", "cividis"],
    ),
    (
        "Sequential",
        [
            "Greys",
            "Purples",
            "Blues",
            "Greens",
            "Oranges",
            "Reds",
            "YlOrBr",
            "YlOrRd",
            "OrRd",
            "PuRd",
            "RdPu",
            "BuPu",
            "GnBu",
            "PuBu",
            "YlGnBu",
            "PuBuGn",
            "BuGn",
            "YlGn",
        ],
    ),
    (
        "Sequential (2)",
        [
            "binary",
            "gist_yarg",
            "gist_gray",
            "gray",
            "bone",
            "pink",
            "spring",
            "summer",
            "autumn",
            "winter",
            "cool",
            "Wistia",
            "hot",
            "afmhot",
            "gist_heat",
            "copper",
        ],
    ),
    (
        "Diverging",
        [
            "PiYG",
            "PRGn",
            "BrBG",
            "PuOr",
            "RdGy",
            "RdBu",
            "RdYlBu",
            "RdYlGn",
            "Spectral",
            "coolwarm",
            "bwr",
            "seismic",
        ],
    ),
    ("Cyclic", ["twilight", "twilight_shifted", "hsv"]),
    (
        "Qualitative",
        [
            "Pastel1",
            "Pastel2",
            "Paired",
            "Accent",
            "Dark2",
            "Set1",
            "Set2",
            "Set3",
            "tab10",
            "tab20",
            "tab20b",
            "tab20c",
        ],
    ),
    (
        "Miscellaneous",
        [
            "flag",
            "prism",
            "ocean",
            "gist_earth",
            "terrain",
            "gist_stern",
            "gnuplot",
            "gnuplot2",
            "CMRmap",
            "cubehelix",
            "brg",
            "gist_rainbow",
            "rainbow",
            "jet",
            "nipy_spectral",
            "gist_ncar",
        ],
    ),
]

all_cmaps = [f"{k}: {cm}" for k, _cmaps in cmaps for cm in _cmaps]


def plot_color_gradients(cmap_category, cmap_list):
    # Create figure and adjust figure height to number of colormaps

    nrows = len(cmap_list)
    figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
    fig, axes = plt.subplots(nrows=nrows, figsize=(6.4, figh))
    fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh, left=0.2, right=0.99)

    axes[0].set_title(cmap_category + " colormaps", fontsize=14)

    gradient = np.linspace(0, 1, 256)
    gradient = np.vstack((gradient, gradient))
    for ax, name in zip(axes, cmap_list):
        ax.imshow(gradient, aspect="auto", cmap=plt.get_cmap(name))
        ax.text(
            -0.01,
            0.5,
            name,
            va="center",
            ha="right",
            fontsize=10,
            transform=ax.transAxes,
        )

    # Turn off *all* ticks & spines, not just the ones with colormaps.
    for ax in axes:
        ax.set_axis_off()


for cmap_category, cmap_list in cmaps:
    plot_color_gradients(cmap_category, cmap_list)

plt.show()
In [ ]:
import adaptive
import matplotlib.colors as colors
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import FloatSlider, IntSlider, interact

from thesis_cover import load_learner, get_cmap

x_size, y_size = xy_size = (14.13, 9.84)



def plot(fname, cmap, min_clip=0.0, max_clip=1.0, exp=1.0, invert=False):
    learner = load_learner(fname)
    xs, ys, zs = data = learner.interpolated_on_grid(1000)
    cmap = cmap.split(": ")[-1] + ("_r" if invert else "")

    zs = get_cmap(cmap, min_clip, max_clip, exp)(zs)
    fig, ax = plt.subplots(figsize=xy_size)
    im = ax.imshow(
        np.rot90(zs),
        extent=(-0.5 * x_size, 0.5 * x_size, -0.5 * y_size, 0.5 * y_size),
        zorder=3,
    )
    plt.show()


interact(
    plot,
    fname=options,
    cmap=all_cmaps,
    min_clip=FloatSlider(min=0, max=1, step=0.05),
    max_clip=FloatSlider(value=1, min=0, max=1, step=0.05),
    exp=FloatSlider(value=1, min=0.1, max=3, step=0.05),
)