Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22d461c

Browse files
committed
added example code to tutorial
1 parent 58d6057 commit 22d461c

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

tutorials/colors/colormapnorms.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import matplotlib.pyplot as plt
4848
import matplotlib.colors as colors
4949
import matplotlib.cbook as cbook
50+
from matplotlib import cm
5051

5152
N = 100
5253
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
@@ -69,6 +70,46 @@
6970
fig.colorbar(pcm, ax=ax[1], extend='max')
7071
plt.show()
7172

73+
###############################################################################
74+
# Centered
75+
# --------
76+
#
77+
# In many cases, data is symmetrical around a center, for example, positive and
78+
# negative anomalies around a center 0. In this case, we would like the center
79+
# to be mapped to 0.5 and the datapoint with the largest deviation from the
80+
# center to be mapped to 1.0, if its value is greater than the center, or -1.0
81+
# otherwise. The norm `.colors.CenteredNorm` creates such a mapping
82+
# automatically. It is well suited to be combined with a divergent colormap
83+
# which uses different colors edges that meet in the center at an unsaturated
84+
# color.
85+
#
86+
# If the center of symmetry is different from 0, it can be set with the
87+
# *vcenter* argument. For logarithmic scaling on both sides of the center, see
88+
# `.colors.SymLogNorm` below; to apply a different mapping above and below the
89+
# center, use `.colors.TwoSlopeNorm` below.
90+
91+
delta = 0.1
92+
x = np.arange(-3.0, 4.001, delta)
93+
y = np.arange(-4.0, 3.001, delta)
94+
X, Y = np.meshgrid(x, y)
95+
Z1 = np.exp(-X**2 - Y**2)
96+
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
97+
Z = (0.9*Z1 - 0.5*Z2) * 2
98+
99+
# select a divergent colormap
100+
cmap = cm.coolwarm
101+
102+
fig, (ax1, ax2) = plt.subplots(ncols=2)
103+
pc = ax1.pcolormesh(Z, cmap=cmap)
104+
fig.colorbar(pc, ax=ax1)
105+
ax1.set_title('Normalize()')
106+
107+
pc = ax2.pcolormesh(Z, norm=colors.CenteredNorm(), cmap=cmap)
108+
fig.colorbar(pc, ax=ax2)
109+
ax2.set_title('CenteredNorm()')
110+
111+
plt.show()
112+
72113
###############################################################################
73114
# Symmetric logarithmic
74115
# ---------------------

0 commit comments

Comments
 (0)