The syntax you're using is completely different to the one in the linked documentation. There is essentially no difference between normalizing a scatter or a pcolor(mesh) or just any other scalar mappable object.
It's always
colormap = plt.cm.bwr #or any other colormap
normalize = matplotlib.colors.Normalize(vmin=-1, vmax=1)
plt.scatter(x, y, c=z, s=5, cmap=colormap, norm=normalize, marker='*')
Answer from ImportanceOfBeingErnest on Stack OverflowMatplotlib
matplotlib.org › stable › users › explain › colors › colormapnorms.html
Colormap normalization — Matplotlib 3.11.2 documentation
will map the data in Z linearly from -1 to +1, so Z=0 will give a color at the center of the colormap RdBu_r (white in this case). Matplotlib does this mapping in two steps, with a normalization from the input data to [0, 1] occurring first, and then mapping onto the indices in the colormap.
Matplotlib
matplotlib.org › 2.0.2 › users › colormapnorms.html
Colormap Normalization — Matplotlib 2.0.2 documentation
For this we use the colors.PowerNorm(). It takes as an argument gamma (gamma == 1.0 will just yield the default linear normalization): ... There should probably be a good reason for plotting the data using this type of transformation. Technical viewers are used to linear and logarithmic axes and data transformations. Power laws are less common, and viewers should explicitly be made aware that they have been used. """ Demonstration of using norm to map colormaps onto data in non-linear ways. """ import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as colors from matplotlib.mlab import bivariate_normal N = 100 X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)] ''' PowerNorm: Here a power-law trend in X partially obscures a rectified sine wave in Y.
Matplotlib
matplotlib.org › stable › api › _as_gen › matplotlib.colors.Normalize.html
matplotlib.colors.Normalize — Matplotlib 3.11.2 documentation
Maps the normalized value (i.e., index in the colormap) back to image data value.
Matplotlib
matplotlib.org › stable › gallery › images_contours_and_fields › colormap_normalizations.html
Colormap normalizations — Matplotlib 3.11.2 documentation
fig, ax = plt.subplots(3, 1, layout='constrained') pcm = ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest', vmin=-np.max(Z)) fig.colorbar(pcm, ax=ax[0], extend='both', orientation='vertical', label='linear scaling') # Evenly-spaced bounds gives a contour-like effect. bounds = np.linspace(-2, 2, 11) norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256) pcm = ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest', norm=norm) fig.colorbar(pcm, ax=ax[1], extend='both', orientation='vertical', label='BoundaryNorm\nlinspace(-2, 2, 11)') # Unevenly-spaced bounds changes the colormapping.
Top answer 1 of 2
28
The syntax you're using is completely different to the one in the linked documentation. There is essentially no difference between normalizing a scatter or a pcolor(mesh) or just any other scalar mappable object.
It's always
colormap = plt.cm.bwr #or any other colormap
normalize = matplotlib.colors.Normalize(vmin=-1, vmax=1)
plt.scatter(x, y, c=z, s=5, cmap=colormap, norm=normalize, marker='*')
2 of 2
0
# define the range of values in the data (100 is arbitrary)
my_range = np.linspace(-1,1,100)
# cmap is a function. It returns (rgba) colors base on a range 0-1. Therefore,
# transform your values to 0-1 to use them as input for cmap()
cmap = cm.get_cmap('viridis', 100)
my_transformed_range = (my_range - np.min(my_range)) / (np.max(my_range) - np.min(my_range))
# colors should be supplied as a single color or a list (here using the cmap fx)
plt.scatter(my_range, np.ones(100), color=[cmap(i) for i in my_transformed_range])
Matplotlib
matplotlib.org › 2.1.0 › tutorials › colors › colormapnorms.html
Colormap Normalization — Matplotlib 2.1.0 documentation
In [1]: import matplotlib as mpl In [2]: norm = mpl.colors.Normalize(vmin=-1.,vmax=1.) In [3]: norm(0.) Out[3]: 0.5 · However, there are sometimes cases where it is useful to map data to colormaps in a non-linear fashion.
TutorialsPoint
tutorialspoint.com › matplotlib › matplotlib_colormap_normalization.htm
Matplotlib - ColorMap Normalization
The TwoSlopeNorm normalization is used when different colormaps on either side of a conceptual center point, it is commonly used in scenarios such as topographic maps where land and ocean have different elevation and depth ranges. When built-in norms fall short, then the FuncNorm enables custom normalization using arbitrary functions. Additionally, Matplotlib supports crafting custom normalizations like MidpointNormalize, useful for defining mappings in specialized visualizations.
Matplotlib
matplotlib.org › 3.1.1 › tutorials › colors › colormapnorms.html
Colormap Normalization — Matplotlib 3.1.2 documentation
May 1, 2020 - In [1]: import matplotlib as mpl In [2]: norm = mpl.colors.Normalize(vmin=-1.,vmax=1.) In [3]: norm(0.) Out[3]: 0.5 · However, there are sometimes cases where it is useful to map data to colormaps in a non-linear fashion.
Matplotlib
matplotlib.org › 2.1.1 › tutorials › colors › colormapnorms.html
Colormap Normalization — Matplotlib 2.1.1 documentation
In [1]: import matplotlib as mpl In [2]: norm = mpl.colors.Normalize(vmin=-1.,vmax=1.) In [3]: norm(0.) Out[3]: 0.5 · However, there are sometimes cases where it is useful to map data to colormaps in a non-linear fashion.
Matplotlib
matplotlib.org › 3.1.0 › tutorials › colors › colormapnorms.html
Colormap Normalization — Matplotlib 3.1.0 documentation
In [1]: import matplotlib as mpl In [2]: norm = mpl.colors.Normalize(vmin=-1.,vmax=1.) In [3]: norm(0.) Out[3]: 0.5 · However, there are sometimes cases where it is useful to map data to colormaps in a non-linear fashion.
Matplotlib
matplotlib.org › 2.0.0 › users › colormapnorms.html
Colormap Normalization — Matplotlib 2.0.0 documentation
For this we use the colors.PowerNorm(). It takes as an argument gamma (gamma == 1.0 will just yield the default linear normalization): ... There should probably be a good reason for plotting the data using this type of transformation. Technical viewers are used to linear and logarithmic axes and data transformations. Power laws are less common, and viewers should explicitly be made aware that they have been used. """ Demonstration of using norm to map colormaps onto data in non-linear ways. """ import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as colors from matplotlib.mlab import bivariate_normal N = 100 X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)] ''' PowerNorm: Here a power-law trend in X partially obscures a rectified sine wave in Y.
Matplotlib
matplotlib.org › 2.1.2 › tutorials › colors › colormapnorms.html
Colormap Normalization — Matplotlib 2.1.2 documentation
In [1]: import matplotlib as mpl In [2]: norm = mpl.colors.Normalize(vmin=-1.,vmax=1.) In [3]: norm(0.) Out[3]: 0.5 · However, there are sometimes cases where it is useful to map data to colormaps in a non-linear fashion.
Matplotlib
matplotlib.org › stable › tutorials › colors › colormapnorms.html
Matplotlib
August 13, 2021 - Matplotlib has support for visualizing information with a wide array of colors and colormaps.
Matplotlib
matplotlib.org › 3.4.1 › tutorials › colors › colormapnorms.html
Colormap Normalization — Matplotlib 3.4.1 documentation
In [1]: import matplotlib as mpl In [2]: norm = mpl.colors.Normalize(vmin=-1, vmax=1) In [3]: norm(0) Out[3]: 0.5 · However, there are sometimes cases where it is useful to map data to colormaps in a non-linear fashion.
Matplotlib
matplotlib.org › 3.5.1 › tutorials › colors › colormapnorms.html
Colormap Normalization — Matplotlib 3.5.1 documentation
In [1]: import matplotlib as mpl In [2]: norm = mpl.colors.Normalize(vmin=-1, vmax=1) In [3]: norm(0) Out[3]: 0.5 · However, there are sometimes cases where it is useful to map data to colormaps in a non-linear fashion.
Mines-paristech
members.cbio.mines-paristech.fr › ~nvaroquaux › tmp › matplotlib › users › colormapnorms.html
Colormap Normalization — Matplotlib 2.0.0b4.post2415.dev0+g315f2c9 documentation
For this we use the colors.PowerNorm(). It takes as an argument gamma (gamma == 1.0 will just yield the default linear normalization): ... There should probably be a good reason for plotting the data using this type of transformation. Technical viewers are used to linear and logarithmic axes and data transformations. Power laws are less common, and viewers should explicitly be made aware that they have been used. """ Demonstration of using norm to map colormaps onto data in non-linear ways. """ import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as colors from matplotlib.mlab import bivariate_normal N = 100 X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)] ''' PowerNorm: Here a power-law trend in X partially obscures a rectified sine wave in Y.
Matplotlib
matplotlib.org › 3.5.0 › tutorials › colors › colormapnorms.html
Colormap Normalization — Matplotlib 3.5.0 documentation
July 17, 2020 - In [1]: import matplotlib as mpl In [2]: norm = mpl.colors.Normalize(vmin=-1, vmax=1) In [3]: norm(0) Out[3]: 0.5 · However, there are sometimes cases where it is useful to map data to colormaps in a non-linear fashion.
Matplotlib
matplotlib.org › 3.10.7 › users › explain › colors › colormapnorms.html
Colormap normalization — Matplotlib 3.10.7 documentation
will map the data in Z linearly from -1 to +1, so Z=0 will give a color at the center of the colormap RdBu_r (white in this case). Matplotlib does this mapping in two steps, with a normalization from the input data to [0, 1] occurring first, and then mapping onto the indices in the colormap.