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

Skip to content

Commit 38cb3dc

Browse files
committed
DOC: updatye docs for DivergingNorm
1 parent 5f85397 commit 38cb3dc

File tree

6 files changed

+73
-14
lines changed

6 files changed

+73
-14
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
:orphan:
2+
3+
New DivergingNorm added
4+
```````````````````````
5+
6+
`colors.DivergingNorm` added to allow a colormap to have unequal data mapping
7+
into the colormap around a center data value. See
8+
:doc:`/gallery/userdemo/colormap_diverging_norm` and
9+
:doc:`/tutorials/colors/colormapnorms`.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
====================================
3+
DivergingNorm Colormap Normalization
4+
====================================
5+
6+
Demonstration of using `.colors.DivergingNorm` to map data onto a colormap
7+
with a different ramp on either side of a central value (zero in this
8+
case).
9+
10+
.. note::
11+
Non-symmetric mapping of data to color is non-standard
12+
practice for quantitative data, and should only be used advisedly. A
13+
practical example is having an ocean/land colormap where the land and
14+
ocean data span different ranges.
15+
16+
"""
17+
18+
import numpy as np
19+
import matplotlib.pyplot as plt
20+
import matplotlib.colors as colors
21+
22+
N = 100
23+
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
24+
Z1 = np.exp(-X**2 - Y**2)
25+
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
26+
Z = (Z1 - 0.4 * Z2) * 10000
27+
28+
fig, ax = plt.subplots(2, 1, constrained_layout=True)
29+
30+
pcm = ax[0].pcolormesh(X, Y, Z,
31+
norm=colors.DivergingNorm(vcenter=0.),
32+
vmin=-4000, vmax=10000,
33+
cmap='RdBu_r')
34+
fig.colorbar(pcm, ax=ax[0], extend='both')
35+
ax[0].set_title('DivergingNorm', loc='left')
36+
37+
pcm = ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', vmin=-10000, vmax=10000)
38+
fig.colorbar(pcm, ax=ax[1], extend='both')
39+
ax[1].set_title('Linear Norm', loc='left')
40+
41+
plt.show()

examples/userdemo/colormap_normalizations_custom.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
"""
22
==============================
3-
Colormap Normalizations Custom
3+
Custom Colormap Normalizations
44
==============================
55
6-
Demonstration of using norm to map colormaps onto data in non-linear ways.
6+
Demonstration of using norm to map datra onto colormaps using a user-defined
7+
normalization. The norm here is the same as the one provided by
8+
`.colors.DivergingNorm`; see
9+
:doc:`/gallery/userdemo/colormap_diverging_norm`.
710
"""
811

912
import numpy as np
1013
import matplotlib.pyplot as plt
1114
import matplotlib.colors as colors
1215

1316
N = 100
14-
'''
15-
Custom Norm: An example with a customized normalization. This one
16-
uses the example above, and normalizes the negative data differently
17-
from the positive.
18-
'''
1917
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
2018
Z1 = np.exp(-X**2 - Y**2)
2119
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
22-
Z = (Z1 - Z2) * 2
20+
Z = (Z1 - 0.4 * Z2) * 10000
2321

2422

2523
# Example of making your own norm. Also see matplotlib.colors.

lib/matplotlib/colorbar.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,9 @@ def _use_auto_colorbar_locator(self):
509509
"""
510510
return (self.boundaries is None
511511
and self.values is None
512-
and ((type(self.norm) == colors.Normalize)
513-
or (type(self.norm) == colors.LogNorm)))
512+
and ((type(self.norm) in [colors.Normalize,
513+
colors.LogNorm,
514+
colors.DivergingNorm])))
514515

515516
def update_ticks(self):
516517
"""
@@ -1009,6 +1010,7 @@ def _locate(self, x):
10091010
# as to make the interpolation more accurate.
10101011
b = self.norm(self._boundaries, clip=False).filled()
10111012
xn = self.norm(x, clip=False).filled()
1013+
b = np.unique(b)
10121014

10131015
# The rest is linear interpolation with extrapolation at ends.
10141016
ii = np.searchsorted(b, xn)

lib/matplotlib/colors.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,9 @@ def __init__(self, vmin=None, vcenter=None, vmax=None):
11141114
11151115
Examples
11161116
--------
1117+
1118+
See :doc:`/gallery/userdemo/colormap_diverging_norm`.
1119+
11171120
This maps data value -4000 to 0., 0 to 0.5, and +10000 to 1.0; data
11181121
between is linearly interpolated::
11191122

tutorials/colors/colormapnorms.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
# cases such as masked data)
199199
#
200200
# .. note::
201-
# This may appear soon as :func:`colors.OffsetNorm`.
201+
# This example is the same as `.colors.DivergingNorm`.
202202
#
203203
# As above, non-symmetric mapping of data to color is non-standard
204204
# practice for quantitative data, and should only be used advisedly. A
@@ -209,7 +209,7 @@
209209
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
210210
Z1 = np.exp(-X**2 - Y**2)
211211
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
212-
Z = (Z1 - Z2) * 2
212+
Z = (Z1 - 0.4 * Z2) * 10000
213213

214214

215215
class MidpointNormalize(colors.Normalize):
@@ -224,13 +224,19 @@ def __call__(self, value, clip=None):
224224
return np.ma.masked_array(np.interp(value, x, y))
225225

226226

227-
fig, ax = plt.subplots(2, 1)
227+
fig, ax = plt.subplots(2, 1, constrained_layout=True)
228228

229229
pcm = ax[0].pcolormesh(X, Y, Z,
230230
norm=MidpointNormalize(midpoint=0.),
231+
vmin=-4000, vmax=10000,
231232
cmap='RdBu_r')
232233
fig.colorbar(pcm, ax=ax[0], extend='both')
234+
ax[0].set_title('Custom Normalization', loc='left')
233235

234-
pcm = ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', vmin=-np.max(Z))
236+
pcm = ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r',
237+
norm=colors.DivergingNorm(vcenter=0.),
238+
vmin=-4000, vmax=10000)
235239
fig.colorbar(pcm, ax=ax[1], extend='both')
240+
ax[1].set_title('DivergingNorm Normalization', loc='left')
236241
fig.show()
242+
plt.show()

0 commit comments

Comments
 (0)