Matplotlib.colors.Normalize class in Python

07 Dec, 2021
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.  
The matplotlib.colors.Normalize 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.Normalize class is used to normalize data into the interval of [0.0, 1.0].Syntax: 
class matplotlib.colors.Normalize(vmin=None, vmax=None, clip=False)
If either of vmin or vmax is not set then it initializes from the minimum and maximum value of the first input processed respectively. In other words, __call__(Data) calls autoscale_None(Data). If the value of clip is set to True and the given value is out of range then it returns 0 or 1, whichever is the closest. If vmax==vmin then it returns 0. It operates with both scalar or arrays including masked arrays. The masked values are set to 1 if clip is True, else they remain masked.Methods:  
autoscale(self, A): This method sets the vmin to min and vmax to max of A. autoscale_None(self, A): This method autoscales only vmin and vmax with None value. inverse(self, value): It interchanges the values of vmin and vmax. static process_value(value): The value parameter in this method can be a scalar or a sequence. It is used to homogenize input values for efficient and simple normalization. This method returns a masked array of matching values. All float data types are preserved and integer data types with two or smaller bytes are transformed to np.float32, while the larger bytes type are transformed into np.float64. This is done to improve speed for larger arrays by preserving float32 values whenever possible through using in-place operations. scaled(self): It returns a boolean to check if vmin or vmax is set.  
autoscale(self, A): This method sets the vmin to min and vmax to max of A. 
autoscale_None(self, A): This method autoscales only vmin and vmax with None value. 
inverse(self, value): It interchanges the values of vmin and vmax. 
static process_value(value): The value parameter in this method can be a scalar or a sequence. It is used to homogenize input values for efficient and simple normalization. This method returns a masked array of matching values. All float data types are preserved and integer data types with two or smaller bytes are transformed to np.float32, while the larger bytes type are transformed into np.float64. This is done to improve speed for larger arrays by preserving float32 values whenever possible through using in-place operations. 
scaled(self): It returns a boolean to check if vmin or vmax is set.  
Example 1:  
Python3
import matplotlib.pyplot as pltimport numpy as npfrom matplotlib import colorsfrom matplotlib.ticker import PercentFormatter # set a  random state for# reproducibilitynp.random.seed(19687581) total_points = 500000total_bins = 100 # Centering at a = 0 and b = 5# generate normal distributionsa = np.random.randn(total_points)b = .4 * a + np.random.randn(500000) + 5 figure, axes = plt.subplots(1, 2,                            tight_layout = True) # C is the count in each binC, bins, patches = axes[0].hist(a,                                bins = total_bins) # We'll color code by height,# but you could use any scalarfracs = C / C.max() # Normalize of  the data to 0..1# for covering the full range of# the colormapnorm = colors.Normalize(fracs.min(), fracs.max()) # looping through the objects and# setting the color of each accordinglyfor thisfrac, thispatch in zip(fracs, patches):    color = plt.cm.viridis(norm(thisfrac))    thispatch.set_facecolor(color) # normalize the inputs by Caxes[1].hist(a, bins = total_bins, density = True) # formating the y-axis for displaying# percentageaxes[1].yaxis.set_major_formatter(PercentFormatter(xmax = 1))
Output:  
Example 2:  
Python3
import matplotlib.pyplot as pltimport matplotlib as mpl figure, axes = plt.subplots(figsize =(6, 1))figure.subplots_adjust(bottom = 0.5) color_map = mpl.cm.coolnormalizer = mpl.colors.Normalize(vmin = 0, vmax = 5) figure.colorbar(mpl.cm.ScalarMappable(norm = normalizer,               cmap = color_map),               cax = axes, orientation ='horizontal',               label ='Arbitrary Units')
Output:  
 
gabaa406
clintra
Python-matplotlib
Python
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Convert integer to string in Python
Convert string to integer in Python
How to set input type date in dd-mm-yyyy format using HTML ?
Python infinity
Similarities and Difference between Java and C++