🌐
Matplotlib
matplotlib.org › 3.3.4 › api › _as_gen › matplotlib.colors.DivergingNorm.html
matplotlib.colors.DivergingNorm — Matplotlib 3.3.4 documentation
class matplotlib.colors.DivergingNorm(**kwargs)[source]¶ · Bases: matplotlib.colors.TwoSlopeNorm · [Deprecated] Notes · Deprecated since version 3.2: Normalize data with a set center. Useful when mapping data with an unequal rates of change around a conceptual center, e.g., data that range ...
🌐
Matplotlib
matplotlib.org › 3.1.0 › api › _as_gen › matplotlib.colors.DivergingNorm.html
matplotlib.colors.DivergingNorm — Matplotlib 3.1.0 documentation
>>> import matplotlib.colors as mcolors >>> offset = mcolors.DivergingNorm(vmin=-4000., vcenter=0., vmax=10000) >>> data = [-4000., -2000., 0., 2500., 5000., 7500., 10000.] >>> offset(data) array([0., 0.25, 0.5, 0.625, 0.75, 0.875, 1.0])
🌐
Matplotlib
matplotlib.org › 3.2.1 › api › _as_gen › matplotlib.colors.DivergingNorm.html
matplotlib.colors.DivergingNorm — Matplotlib 3.2.1 documentation
class matplotlib.colors.DivergingNorm(**kwargs)[source]¶ · Bases: matplotlib.colors.TwoSlopeNorm · [Deprecated] Notes ·
🌐
Matplotlib
matplotlib.org › 3.1.0 › gallery › userdemo › colormap_normalizations_diverging.html
DivergingNorm colormap normalization — Matplotlib 3.1.0 documentation
May 18, 2019 - import numpy as np import ... # make the norm: Note the center is offset so that the land has more # dynamic range: divnorm = colors.DivergingNorm(vmin=-500, vcenter=0, vmax=4000) pcm = ax.pcolormesh(longitude, latitude, topo, rasterized=True, norm=divnorm, ...
🌐
Matplotlib
matplotlib.org › 3.1.1 › api › _as_gen › matplotlib.colors.DivergingNorm.html
matplotlib.colors.DivergingNorm — Matplotlib 3.1.2 documentation
>>> import matplotlib.colors as mcolors >>> offset = mcolors.DivergingNorm(vmin=-4000., vcenter=0., vmax=10000) >>> data = [-4000., -2000., 0., 2500., 5000., 7500., 10000.] >>> offset(data) array([0., 0.25, 0.5, 0.625, 0.75, 0.875, 1.0])
🌐
Matplotlib
matplotlib.org › 3.1.3 › gallery › userdemo › colormap_normalizations_diverging.html
DivergingNorm colormap normalization — Matplotlib 3.1.3 documentation
import numpy as np import ... # make the norm: Note the center is offset so that the land has more # dynamic range: divnorm = colors.DivergingNorm(vmin=-500, vcenter=0, vmax=4000) pcm = ax.pcolormesh(longitude, latitude, topo, rasterized=True, norm=divnorm, ...
🌐
GitHub
github.com › matplotlib › matplotlib › issues › 15336
DivergingNorm is a misleading name · Issue #15336 · matplotlib/matplotlib
September 24, 2019 - DivergingNorm was introduced when #12419 was merged. Discussion of #15333 on the Sept. 24, 2019 call led to the conclusion that the "DivergingNorm" name was an unfortunate and misleading ...
Author: matplotlib
🌐
GitHub
github.com › matplotlib › matplotlib › issues › 14892
Doc: MidpointNormalize example should point to DivergingNorm as a built-in function · Issue #14892 · matplotlib/matplotlib
July 26, 2019 - I've seen many folks seeking a way to set 0 as the midpoint in a color bar. I wonder if this MidpointNormalize subclass could be added into matplotlib as a builtin function. The following code is copied from the doc. import numpy as np i...
Author: matplotlib
Find elsewhere
🌐
Matplotlib
matplotlib.org › 3.1.1 › gallery › userdemo › colormap_normalizations_diverging.html
DivergingNorm colormap normalization — Matplotlib 3.1.2 documentation
January 5, 2020 - import numpy as np import ... # make the norm: Note the center is offset so that the land has more # dynamic range: divnorm = colors.DivergingNorm(vmin=-500, vcenter=0, vmax=4000) pcm = ax.pcolormesh(longitude, latitude, topo, rasterized=True, norm=divnorm, ...
🌐
Readthedocs
proplot.readthedocs.io › en › v0.9.3 › api › proplot.colors.DivergingNorm.html
DivergingNorm — ProPlot documentation
class DivergingNorm(vcenter=0, vmin=None, vmax=None, fair=True, clip=None)[source]¶ · Bases: matplotlib.colors.Normalize · Normalizer that ensures some central data value lies at the central colormap color. The default central value is 0. Parameters · vcenter (float, optional) – The data ...
🌐
Matplotlib
matplotlib.org › 3.1.3 › api › _as_gen › matplotlib.colors.DivergingNorm.html
matplotlib.colors.DivergingNorm — Matplotlib 3.1.3 documentation
>>> import matplotlib.colors as mcolors >>> offset = mcolors.DivergingNorm(vmin=-4000., vcenter=0., vmax=10000) >>> data = [-4000., -2000., 0., 2500., 5000., 7500., 10000.] >>> offset(data) array([0., 0.25, 0.5, 0.625, 0.75, 0.875, 1.0])
🌐
GitHub
github.com › matplotlib › matplotlib › pull › 15333
Enh: DivergingNorm Fair by ImportanceOfBeingErnest · Pull Request #15333 · matplotlib/matplotlib
I agree with @ImportanceOfBeingErnest that a truncated but equally spaced around a center color colormap can often be the correct mapping for a DivergingNorm. I think I even have a use case from this summer: mapping errors where they skewed so extreme in one direction that putting both sides on the cmap would end up with a ton of wasted space (it was like a 10 to -1 spread) that would more accurately be conveyed with the proposed cmap. ... import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import DivergingNorm np.random.seed(19680801) data = np.random.rand(4, 11) - 0.25
Author: matplotlib
🌐
GitHub
github.com › matplotlib › matplotlib › pull › 12419
Add DivergingNorm (again, again, again) by jklymak · Pull Request #12419 · matplotlib/matplotlib
import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as mcolors x = np.linspace(-2, 7) y = np.linspace(-1*np.pi, np.pi) X, Y = np.meshgrid(x, y) Z = x * np.sin(Y)**2 fig, (ax1, ax2) = plt.subplots(ncols=2) cmap = plt.cm.coolwarm cmap.set_over('orange') cmap.set_under('steelblue') norm = mcolors.DivergingNorm(vmin=-1, vcenter=0, vmax=6) img1 = ax1.pcolormesh(Z, cmap=cmap, norm=None) cbar1 = fig.colorbar(img1, ax=ax1, extend='neither') img2 = ax2.pcolormesh(Z, cmap=cmap, norm=norm) cbar2 = fig.colorbar(img2, ax=ax2, extend='both') plt.show()
Author: matplotlib
🌐
Matplotlib
matplotlib.org › 3.2.2 › gallery › userdemo › colormap_normalizations_diverging.html
TwoSlopeNorm colormap normalization — Matplotlib 3.2.2 documentation
Click here to download the full example code · Sometimes we want to have a different colormap on either side of a conceptual center point, and we want those two colormaps to have different linear scales. An example is a topographic map where the land and ocean have a center at zero, but land ...
🌐
Matplotlib
matplotlib.org › 3.1.0 › _downloads › 7d6b8aef761fe2376554b6c454472347 › colormap_normalizations_diverging.py
https://matplotlib.org/3.1.0/_downloads/7d6b8aef76...
""" ===================================== DivergingNorm colormap normalization ===================================== Sometimes we want to have a different colormap on either side of a conceptual center point, and we want those two colormaps to have different linear scales.
🌐
Readthedocs
proplot.readthedocs.io › en › v0.6.4 › _modules › proplot › colors.html
proplot.colors — ProPlot documentation
# https://github.com/matplotlib/matplotlib/pull/15333#issuecomment-537545430 # Since proplot is starting without matplotlib's baggage we can just implement # DivergingNorm like they would prefer if they didn't have to worry about # confusing users: single class, default "fair" scaling that can be turned off.
Top answer
1 of 1
56

First off, there's more than one way to do this.

  1. Pass an instance of DivergingNorm as the norm kwarg.
  2. Use the colors kwarg to contourf and manually specify the colors
  3. Use a discrete colormap constructed with matplotlib.colors.from_levels_and_colors.

The simplest way is the first option. It is also the only option that allows you to use a continuous colormap.

The reason to use the first or third options is that they will work for any type of matplotlib plot that uses a colormap (e.g. imshow, scatter, etc).

The third option constructs a discrete colormap and normalization object from specific colors. It's basically identical to the second option, but it will a) work with other types of plots than contour plots, and b) avoids having to manually specify the number of contours.

As an example of the first option (I'll use imshow here because it makes more sense than contourf for random data, but contourf would have identical usage other than the interpolation option.):

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

data = np.random.random((10,10))
data = 10 * (data - 0.8)

fig, ax = plt.subplots()
im = ax.imshow(data, norm=DivergingNorm(0), cmap=plt.cm.seismic, interpolation='none')
fig.colorbar(im)
plt.show()

As an example of the third option (notice that this gives a discrete colormap instead of a continuous colormap):

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

data = np.random.random((10,10))
data = 10 * (data - 0.8)

num_levels = 20
vmin, vmax = data.min(), data.max()
midpoint = 0
levels = np.linspace(vmin, vmax, num_levels)
midp = np.mean(np.c_[levels[:-1], levels[1:]], axis=1)
vals = np.interp(midp, [vmin, midpoint, vmax], [0, 0.5, 1])
colors = plt.cm.seismic(vals)
cmap, norm = from_levels_and_colors(levels, colors)

fig, ax = plt.subplots()
im = ax.imshow(data, cmap=cmap, norm=norm, interpolation='none')
fig.colorbar(im)
plt.show()