Matplotlib Colormap Normalization

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In data visualization, colormaps are used to represent numerical data through color. However, sometimes the data distribution may be nonlinear, which can make it difficult to discern the details of the data. In such cases, colormap normalization can be used to map colormaps onto data in nonlinear ways to help visualize the data more accurately. Matplotlib provides several normalization methods, including SymLogNorm and AsinhNorm, which can be used to normalize colormaps. This lab will demonstrate how to use SymLogNorm and AsinhNorm to map colormaps onto nonlinear data.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48611{{"`Matplotlib Colormap Normalization`"}} matplotlib/figures_axes -.-> lab-48611{{"`Matplotlib Colormap Normalization`"}} python/variables_data_types -.-> lab-48611{{"`Matplotlib Colormap Normalization`"}} python/numeric_types -.-> lab-48611{{"`Matplotlib Colormap Normalization`"}} python/booleans -.-> lab-48611{{"`Matplotlib Colormap Normalization`"}} python/lists -.-> lab-48611{{"`Matplotlib Colormap Normalization`"}} python/tuples -.-> lab-48611{{"`Matplotlib Colormap Normalization`"}} python/dictionaries -.-> lab-48611{{"`Matplotlib Colormap Normalization`"}} python/function_definition -.-> lab-48611{{"`Matplotlib Colormap Normalization`"}} python/importing_modules -.-> lab-48611{{"`Matplotlib Colormap Normalization`"}} python/numerical_computing -.-> lab-48611{{"`Matplotlib Colormap Normalization`"}} python/data_visualization -.-> lab-48611{{"`Matplotlib Colormap Normalization`"}} python/build_in_functions -.-> lab-48611{{"`Matplotlib Colormap Normalization`"}} end

Import Required Libraries

In this step, we will import the necessary libraries, including Matplotlib, NumPy, and Matplotlib colors.

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as colors

Create Synthetic Data

In this step, we will create a synthetic dataset consisting of two humps, one negative and one positive, with the positive hump having an amplitude eight times greater than the negative hump. We will then apply SymLogNorm to visualize the data.

def rbf(x, y):
    return 1.0 / (1 + 5 * ((x ** 2) + (y ** 2)))

N = 200
gain = 8
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
Z1 = rbf(X + 0.5, Y + 0.5)
Z2 = rbf(X - 0.5, Y - 0.5)
Z = gain * Z1 - Z2

shadeopts = {'cmap': 'PRGn', 'shading': 'gouraud'}
colormap = 'PRGn'
lnrwidth = 0.5

Apply SymLogNorm

In this step, we will apply SymLogNorm to the synthetic data and visualize the results.

fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)

pcm = ax[0].pcolormesh(X, Y, Z,
                       norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
                                              vmin=-gain, vmax=gain, base=10),
                       **shadeopts)
fig.colorbar(pcm, ax=ax[0], extend='both')
ax[0].text(-2.5, 1.5, 'symlog')

pcm = ax[1].pcolormesh(X, Y, Z, vmin=-gain, vmax=gain,
                       **shadeopts)
fig.colorbar(pcm, ax=ax[1], extend='both')
ax[1].text(-2.5, 1.5, 'linear')

plt.show()

Apply AsinhNorm

In this step, we will apply AsinhNorm to the synthetic data and visualize the results.

fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)

pcm = ax[0].pcolormesh(X, Y, Z,
                       norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
                                              vmin=-gain, vmax=gain, base=10),
                       **shadeopts)
fig.colorbar(pcm, ax=ax[0], extend='both')
ax[0].text(-2.5, 1.5, 'symlog')

pcm = ax[1].pcolormesh(X, Y, Z,
                       norm=colors.AsinhNorm(linear_width=lnrwidth,
                                             vmin=-gain, vmax=gain),
                       **shadeopts)
fig.colorbar(pcm, ax=ax[1], extend='both')
ax[1].text(-2.5, 1.5, 'asinh')

plt.show()

Summary

In this lab, we learned how to use SymLogNorm and AsinhNorm to map colormaps onto nonlinear data. By applying these normalization methods, we can visualize the data more accurately and discern the details of the data more easily.

Other Python Tutorials you may like