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

Skip to content

Commit 277eca7

Browse files
committed
Create colormap_normalizations.py
Example with colormap normalizations
1 parent 41a8021 commit 277eca7

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
"""
2+
Demonstration of using norm to map colormaps onto data in non-linear ways.
3+
"""
4+
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
import matplotlib.colors as colors
8+
from matplotlib.mlab import bivariate_normal
9+
10+
'''
11+
Lognorm: Instead of pcolor log10(Z1) you can have colorbars that have
12+
the exponential labels using a norm.
13+
'''
14+
N = 100
15+
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
16+
17+
# A low hump with a spike coming out of the top right. Needs to have
18+
# z/colour axis on a log scale so we see both hump and spike. linear
19+
# scale only shows the spike.
20+
Z1 = bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0) + 0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
21+
22+
fig,ax=plt.subplots(2,1)
23+
24+
pcm=ax[0].pcolor(X, Y, Z1, norm=colors.LogNorm(vmin=Z1.min(), vmax=Z1.max()), cmap='PuBu_r')
25+
fig.colorbar(pcm,ax=ax[0],extend='max')
26+
27+
pcm=ax[1].pcolor(X, Y, Z1, cmap='PuBu_r')
28+
fig.colorbar(pcm,ax=ax[1],extend='max')
29+
fig.show()
30+
31+
32+
'''
33+
PowerNorm: Here a power-law trend in X partially obscures a rectified
34+
sine wave in Y. We can remove the power law using a PowerNorm.
35+
'''
36+
X, Y = np.mgrid[0:3:complex(0, N), 0:2:complex(0, N)]
37+
Z1 = (1+np.sin(Y*10.))*X**(2.)
38+
39+
fig,ax=plt.subplots(2,1)
40+
41+
pcm=ax[0].pcolormesh(X, Y, Z1, norm=colors.PowerNorm(gamma=1./2.), cmap='PuBu_r')
42+
fig.colorbar(pcm,ax=ax[0],extend='max')
43+
44+
pcm=ax[1].pcolormesh(X, Y, Z1, cmap='PuBu_r')
45+
fig.colorbar(pcm,ax=ax[1],extend='max')
46+
fig.show()
47+
48+
49+
'''
50+
SymLogNorm: two humps, one negative and one positive, The positive
51+
with 5-times the amplitude. Linearly, you cannot see detail in the
52+
negative hump. Here we logarithmically scale the positive and
53+
negative data separately.
54+
55+
Note that colorbar labels do not come out looking very good.
56+
'''
57+
58+
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
59+
Z1 = (bivariate_normal(X, Y, 1.,1., 1.0, 1.0))**2 \
60+
- 0.4 * (bivariate_normal(X, Y, 1.0, 1.0, -1.0, 0.0))**2
61+
Z1 = Z1/0.03
62+
63+
fig,ax=plt.subplots(2,1)
64+
65+
pcm=ax[0].pcolormesh(X, Y, Z1,
66+
norm=colors.SymLogNorm(linthresh=0.03,linscale=0.03,
67+
vmin=-1.0,vmax=1.0),
68+
cmap='RdBu_r')
69+
fig.colorbar(pcm,ax=ax[0],extend='both')
70+
71+
pcm=ax[1].pcolormesh(X, Y, Z1, cmap='RdBu_r',vmin=-np.max(Z1))
72+
fig.colorbar(pcm,ax=ax[1],extend='both')
73+
fig.show()
74+
75+
76+
'''
77+
Custom Norm: An example with a customized normalization. This one
78+
uses the example above, and normalizes the negative data differently
79+
from the positive.
80+
'''
81+
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
82+
Z1 = (bivariate_normal(X, Y, 1.,1., 1.0, 1.0))**2 \
83+
- 0.4 * (bivariate_normal(X, Y, 1.0, 1.0, -1.0, 0.0))**2
84+
Z1 = Z1/0.03
85+
86+
# Example of making your own norm. Also see matplotlib.colors.
87+
# From Joe Kington: This one gives two different linear ramps:
88+
class MidpointNormalize(colors.Normalize):
89+
def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
90+
self.midpoint = midpoint
91+
colors.Normalize.__init__(self, vmin, vmax, clip)
92+
def __call__(self, value, clip=None):
93+
# I'm ignoring masked values and all kinds of edge cases to make a
94+
# simple example...
95+
x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
96+
return np.ma.masked_array(np.interp(value, x, y))
97+
#####
98+
fig,ax=plt.subplots(2,1)
99+
100+
pcm=ax[0].pcolormesh(X, Y, Z1,
101+
norm=MidpointNormalize(midpoint=0.),
102+
cmap='RdBu_r')
103+
fig.colorbar(pcm,ax=ax[0],extend='both')
104+
105+
pcm=ax[1].pcolormesh(X, Y, Z1, cmap='RdBu_r',vmin=-np.max(Z1))
106+
fig.colorbar(pcm,ax=ax[1],extend='both')
107+
fig.show()
108+
109+
'''
110+
BoundaryNorm: For this one you provide the boundaries for your colors,
111+
and the Norm puts the first color in between the first pair, the
112+
second color between the second pair, etc.
113+
'''
114+
115+
fig,ax=plt.subplots(3,1,figsize=(8,8))
116+
ax=ax.flatten()
117+
# even bounds gives a contour-like effect
118+
bounds= np.linspace(-1,1,10)
119+
pcm=ax[0].pcolormesh(X, Y, Z1,
120+
norm=colors.BoundaryNorm(boundaries=bounds,ncolors=256),
121+
cmap='RdBu_r')
122+
fig.colorbar(pcm,ax=ax[0],extend='both',orientation='vertical')
123+
124+
# uneven bounds changes the colormapping:
125+
bounds = np.array([-0.25,-0.125,0,0.5,1])
126+
pcm=ax[1].pcolormesh(X, Y, Z1,
127+
norm=colors.BoundaryNorm(boundaries=bounds,ncolors=256),
128+
cmap='RdBu_r')
129+
fig.colorbar(pcm,ax=ax[1],extend='both',orientation='vertical')
130+
131+
pcm=ax[2].pcolormesh(X, Y, Z1, cmap='RdBu_r',vmin=-np.max(Z1))
132+
fig.colorbar(pcm,ax=ax[2],extend='both',orientation='vertical')
133+
fig.show()

0 commit comments

Comments
 (0)