๐ŸŒ
Matplotlib
matplotlib.org โ€บ stable โ€บ gallery โ€บ images_contours_and_fields โ€บ colormap_normalizations.html
Colormap normalizations โ€” Matplotlib 3.11.2 documentation
Demonstration of using norm to map colormaps onto data in non-linear ways. import matplotlib.pyplot as plt import numpy as np import matplotlib.colors as colors N = 100
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ matplotlib โ€บ matplotlib_colormap_normalization.htm
Matplotlib - ColorMap Normalization
The default behavior in Matplotlib is to linearly map colors based on data values within a specified range. This range is typically defined by the minimum (vmin) and maximum (vmax) values of the matplotlib.colors.Normalize() instance arguments.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ matplotlib-colors-normalize-class-in-python
Matplotlib.colors.Normalize class in Python | GeeksforGeeks
April 28, 2025 - The matplotlib.colors module is ... 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:...
๐ŸŒ
Matplotlib
matplotlib.org โ€บ stable โ€บ gallery โ€บ scales โ€บ power_norm.html
Exploring normalizations โ€” Matplotlib 3.11.2 documentation
np.random.seed(19680801) data = np.vstack([ multivariate_normal([10, 10], [[3, 2], [2, 3]], size=100000), multivariate_normal([30, 20], [[3, 1], [1, 3]], size=1000) ]) gammas = [0.8, 0.5, 0.3] fig, 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): ax.set_title(r'Power law $(\gamma=%1.1f)$' % gamma) ax.hist2d(data[:, 0], data[:, 1], bins=100, norm=mcolors.PowerNorm(gamma)) fig.tight_layout() plt.show()
๐ŸŒ
Astropy
docs.astropy.org โ€บ en โ€บ stable โ€บ visualization โ€บ normalization.html
Image stretching and normalization โ€” Astropy v8.0.2
The inputs to the ImageNormalize class can also be the vmin and vmax limits, which you can determine from the Intervals and Normalization classes, and the stretch object: import numpy as np import matplotlib.pyplot as plt from astropy.visualization import (MinMaxInterval, SqrtStretch, ImageNormalize) # Generate a test image image = np.arange(65536).reshape((256, 256)) # Create interval object interval = MinMaxInterval() vmin, vmax = interval.get_limits(image) # Create an ImageNormalize object using a SqrtStretch object norm = ImageNormalize(vmin=vmin, vmax=vmax, stretch=SqrtStretch()) # Display the image fig, ax = plt.subplots() im = ax.imshow(image, origin='lower', norm=norm) fig.colorbar(im)
Top answer
1 of 3
18

I realise that the poster's issue has already been resolved, but the question of normalizing the colors was never dealt with. Since I've figured out how I thought I'd just drop this here for anyone else who might need it.

First you create a norm and pass that to the plotting function, I've tried to add this to the OP's code.

from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d, Axes3D
import pylab as p
import matplotlib

vima=0.5

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(0, 16.67, vima)
Y = np.arange(0, 12.5, vima)
X, Y = np.meshgrid(X, Y)

Z = np.sqrt(((1.2*Y+0.6*X)**2+(0.2*Y+1.6*X)**2)/(0.64*Y**2+0.36*X**2))
Z = np.nan_to_num(Z)

# Make the norm
norm = matplotlib.colors.Normalize(vmin = np.min(Z), vmax = np.max(Z), clip = False)

# Plot with the norm
surf = ax.plot_surface(X, Y, Z,rstride=1, cstride=1, norm=norm, alpha=1,cmap=cm.jet,     linewidth=0)
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

The norm works the same way for the "imshow" command.

2 of 3
6

As JoshAdel noted in a comment (credit belongs to him), it appears that the surface plot is improperly ranging the colormap when a NaN is in the Z array. A simple work-around is to simply convert the NaN's to zero or very large or very small numbers so that the colormap can be normalized to the z-axis range.

from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d, Axes3D
import pylab as p

vima=0.5

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(0, 16.67, vima)
Y = np.arange(0, 12.5, vima)
X, Y = np.meshgrid(X, Y)

Z = np.sqrt(((1.2*Y+0.6*X)**2+(0.2*Y+1.6*X)**2)/(0.64*Y**2+0.36*X**2))
Z = np.nan_to_num(Z) # added this line

surf = ax.plot_surface(X, Y, Z,rstride=1, cstride=1, alpha=1,cmap=cm.jet,  linewidth=0)
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()