<jupyter_start><jupyter_text>Utilities for color maps===============**<NAME>**8/12/2017  **This is a short walk-through of useful plotting utilities available in sidpy**Introduction--------------Some of the functions in ``sidpy.viz.plot_utils`` fill gaps in the default matplotlib package, some weredeveloped for scientific applications, These functions have been developedto substantially simplify the generation of high quality figures for journal publications. Import necessary packages:<jupyter_code># Ensure python 3 compatibility:
from __future__ import division, print_function, absolute_import, \
    unicode_literals
import numpy as np
from warnings import warn
import matplotlib.pyplot as plt
import subprocess
import sys


def install(package):
    subprocess.call([sys.executable, "-m", "pip", "install", package])


# Package for downloading online files:
try:
    import sidpy
except ImportError:
    warn('sidpy not found.  Will install with pip.')
    import pip

    install('sidpy')
    import sidpy<jupyter_output><empty_output><jupyter_text>plot_utils has a handful of colormaps suited for different applications. cmap_jet_white_center()This is the standard jet colormap with a white center instead of green. This is a good colormap for images withdivergent data (data that diverges slightly both positively and negatively from the mean).One example target is the ronchigrams from scanning transmission electron microscopy (STEM) cmap_hot_desaturated()This is a desaturated version of the  standard jet colormap discrete_cmap()This function helps create a discretized version of the provided colormap. This is ideally suited when the dataonly contains a few discrete values. One popular application is the visualization of labels from a clusteringalgorithm<jupyter_code>x_vec = np.linspace(0, 2*np.pi, 256)
y_vec = np.sin(x_vec)

test = y_vec * np.atleast_2d(y_vec).T

fig, axes = plt.subplots(ncols=2, nrows=2, figsize=(10, 10))
for axis, title, cmap in zip(axes.flat,
                             ['Jet',
                              'Jet with white center',
                              'Jet desaturated',
                              'Jet discretized'],
                             [plt.cm.jet,
                              sidpy.viz.plot_utils.cmap_jet_white_center(),
                              sidpy.viz.plot_utils.cmap_hot_desaturated(),
                              sidpy.viz.plot_utils.discrete_cmap(8, cmap='jet')]):
    im_handle = axis.imshow(test, cmap=cmap)
    cbar = plt.colorbar(im_handle, ax=axis, orientation='vertical',
                        fraction=0.046, pad=0.04, use_gridspec=True)
    axis.set_title(title)
fig.tight_layout()<jupyter_output><empty_output><jupyter_text>make_linear_alpha_cmap()On certain occasions we may want to superimpose one image with another. However, this is not possibleby default since colormaps involve solid colors. This function allows one to plot multiple images usinga transparent-to-solid colormap. Here we will demonstrate this by plotting blobs representing atomic columnsover some background intensity.<jupyter_code>num_pts = 256

fig, axis = plt.subplots()

# Prepare some background signal
x_mat, y_mat = np.meshgrid(np.linspace(-0.2*np.pi, 0.1*np.pi, num_pts), np.linspace(0, 0.25*np.pi, num_pts))
background_distortion = 0.2 * (x_mat + y_mat + np.sin(0.25 * np.pi * x_mat))

# plot this signal in grey
axis.imshow(background_distortion, cmap='Greys')

# prepare the signal of interest (think of this as intensities in a HREM dataset)
x_vec = np.linspace(0, 6*np.pi, num_pts)
y_vec = np.sin(x_vec)**2
atom_intensities = y_vec * np.atleast_2d(y_vec).T

# prepare the transparent-to-solid colormap
solid_color = plt.cm.jet(0.8)
translucent_colormap = sidpy.viz.plot_utils.make_linear_alpha_cmap('my_map', solid_color,
                                                              1, min_alpha=0, max_alpha=1)

# plot the atom intensities using the custom colormap
im_handle = axis.imshow(atom_intensities, cmap=translucent_colormap)
cbar = plt.colorbar(im_handle, ax=axis, orientation='vertical',
                    fraction=0.046, pad=0.04, use_gridspec=True)<jupyter_output><empty_output><jupyter_text>get_cmap_object()This function is useful more for developers writing their own plotting functions that need to manipulate thecolormap object. This function makes it easy to ensure that you are working on the colormap object and not thestring name of the colormap (both of which are accepted by most matplotlib functions).Here we simply compare the returned values when passing both the colormap object and the string name of the colormap<jupyter_code>sidpy.viz.plot_utils.get_cmap_object('jet') == sidpy.viz.plot_utils.get_cmap_object(plt.cm.jet)<jupyter_output><empty_output><jupyter_text>cmap_from_rgba()This function is handy for converting a Matlab-style colormap instructions (lists of [reg, green, blue, alpha]) tomatplotlib's style:<jupyter_code>hot_desaturated = [(255.0, (255, 76, 76, 255)),
                   (218.5, (107, 0, 0, 255)),
                   (182.1, (255, 96, 0, 255)),
                   (145.6, (255, 255, 0, 255)),
                   (109.4, (0, 127, 0, 255)),
                   (72.675, (0, 255, 255, 255)),
                   (36.5, (0, 0, 91, 255)),
                   (0, (71, 71, 219, 255))]

new_cmap = sidpy.viz.plot_utils.cmap_from_rgba('hot_desaturated', hot_desaturated, 255)

x_vec = np.linspace(0, 2*np.pi, 256)
y_vec = np.sin(x_vec)

test = y_vec * np.atleast_2d(y_vec).T

fig, axes = plt.subplots(ncols=2, figsize=(10, 5))
for axis, title, cmap in zip(axes.flat,
                             ['Jet', 'Jet desaturated'],
                             [plt.cm.jet, new_cmap]):
    im_handle = axis.imshow(test, cmap=cmap)
    cbar = plt.colorbar(im_handle, ax=axis, orientation='vertical',
                        fraction=0.046, pad=0.04, use_gridspec=True)
    axis.set_title(title)
fig.tight_layout()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1, 'fraction_numerical': 0.03031825795644891, 'mean_word_length': 3.4229629629629628, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '600658', 'license': 'MIT', 'n_tokens_mistral': 2009, 'n_tokens_neox': 1831, 'n_words': 547, 'path': 'notebooks/02_visualization/plot_cmap.ipynb', 'repo_name': 'ondrejdyck/sidpy', 'size': '5977'}