Skip to content
Related Articles

Related Articles

Matplotlib.colors.DivergingNorm class in Python
  • Last Updated : 21 Apr, 2020

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.DivergingNorm 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.DivergingNorm class is very useful while mapping data with uneven or unequal rate of change around a conceptual center. For instance data range between -2 to, with 0 as the center or mid-point.

Syntax: matplotlib.colors.DivergingNorm(vcenter, vmin, vmax)


  1. vcenter: It acceots a float valuethat defines the 0.5 data value in the normalization.
  2. vmin: It is an optional parameter that accepts float values and defines 0.0 data value in normalization which defaults to the minimum value of dataset.
  3. vmax: It is an optional parameter that accepts float values and defines 1.0 data value in normalization which defaults to the maximum value of dataset.

Example 1:

import numpy
from matplotlib import pyplot as plt
from matplotlib import colors
# dummy data to plot
x = numpy.linspace(0, 2*numpy.pi, 30)
y = numpy.linspace(0, 2*numpy.pi, 20)
[A, B] = numpy.meshgrid(x, y)
Q = numpy.sin(A)*numpy.cos(B)
fig = plt.figure()
#  yellow to green to red 
# colormap
ax = fig.add_subplot(1, 2, 1)
plt.pcolor(A, B, Q)
ax = fig.add_subplot(1, 2, 2)
# defining the scale, with white
# at zero
vmin = -0.2
vmax = 0.8
norms = colors.DivergingNorm(vmin=vmin,
plt.pcolor(A, B, Q, 



Example 2:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import matplotlib.colors as colors
file = cbook.get_sample_data('topobathy.npz',
                             asfileobj = False)
with np.load(file) as example:
    topo = example['topo']
    longi = example['longitude']
    latit = example['latitude']
figure, axes = plt.subplots(constrained_layout = True)
# creating a colormap that
# has land and ocean clearly
# delineated and of the
# same length (256 + 256)
undersea =, 0.17, 256))
land =, 1, 256))
every_colors = np.vstack((undersea, land))
terrain_map = colors.LinearSegmentedColormap.from_list('terrain_map',
# the center is offset so that 
# the land has more dynamic range
# while making the norm
diversity_norm = colors.DivergingNorm(vmin =-500
                                      vcenter = 0,
                                      vmax = 4000)
pcm = axes.pcolormesh(longi, latit, topo, 
                      rasterized = True
                      norm = diversity_norm,
                      cmap = terrain_map, )
axes.set_xlabel('Longitude $[^o E]$')
axes.set_ylabel('Latitude $[^o N]$')
axes.set_aspect(1 / np.cos(np.deg2rad(49)))
figure.colorbar(pcm, shrink = 0.6
                extend ='both',
                label ='Elevation [m]')


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :