Open In App

Matplotlib.colors.PowerNorm class in Python

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.
 

matplotlib.colors.PowerNor

The matplotlib.colors.PowerNorm class belongs to the matplotlib.colors module. The matplotlib.colors module is used for converting color or numbers arguments to RGBA or RGB.This module is used for mapping numbers to colors or color specification conversion in a 1-D array of colors also known as colormap.
The matplotlib.colors.PowerNorm class is used to linearly map values to the range of – and after that apply a power-law normalization over the range. Its base class is matplotlib.colors.Normalize.
Methods of the class: 
 



Example 1: 
 




import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np
from numpy.random import multivariate_normal
 
# data for reproducibility
data = np.vstack([
    multivariate_normal([10, 10],
                        [[3, 2],
                         [2, 3]],
                        size = 100000),
     
    multivariate_normal([30, 20],
                        [[2, 3],
                         [1, 3]],
                        size = 1000)
])
 
gammas_array = [0.9, 0.6, 0.4]
 
figure, axs = plt.subplots(nrows = 2,
                           ncols = 2)
 
axs[0, 0].set_title('Linear normalization')
axs[0, 0].hist2d(data[:, 0],
                 data[:, 1],
                 bins = 100)
 
for ax, gamma in zip(axs.flat[1:],
                     gammas_array):
     
    ax.set_title(r'Power law $(\gamma =% 1.1f)$' % gamma)
    ax.hist2d(data[:, 0],
              data[:, 1],
              bins = 100,
              norm = mcolors.PowerNorm(gamma))
 
figure.tight_layout()
 
plt.show()

Output: 
 



Example 2: 
 




import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
 
max_N = 100
A, B = np.mgrid[-3:3:complex(0, max_N),
                -2:2:complex(0, max_N)]
 
 
# PowerNorm: using power-law
# trend in X
A, B = np.mgrid[0:3:complex(0, max_N),
                0:2:complex(0, max_N)]
 
X1 = (1 + np.sin(B * 10.)) * A**(2.)
 
figure, axes = plt.subplots(2, 1)
 
pcm = axes[0].pcolormesh(A, B, X1,
                         norm = colors.PowerNorm(gamma = 1./2.),
                         cmap ='PuBu_r')
 
figure.colorbar(pcm, ax = axes[0],
                extend ='max')
 
pcm = axes[1].pcolormesh(A, B, X1,
                         cmap ='PuBu_r')
 
figure.colorbar(pcm, ax = axes[1],
                extend ='max')
 
plt.show()

Output: 
 

 


Article Tags :