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

Skip to content

Commit d148756

Browse files
committed
Improved examples, created a new file for generating sample data.'
1 parent de62491 commit d148756

File tree

3 files changed

+188
-129
lines changed

3 files changed

+188
-129
lines changed

doc/users/plotting/examples/colormap_normalizations_piecewisenorm.py

Lines changed: 0 additions & 129 deletions
This file was deleted.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""
2+
============================================
3+
Examples of arbitrary colormap normalization
4+
============================================
5+
6+
Here I plot an image array with data spanning for a large dynamic range,
7+
using different normalizations. Look at how each of them enhances
8+
different features.
9+
10+
"""
11+
12+
from mpl_toolkits.mplot3d import Axes3D
13+
import matplotlib.colors as colors
14+
import matplotlib.pyplot as plt
15+
import matplotlib.cm as cm
16+
import numpy as np
17+
from sampledata import PiecewiseNormData
18+
19+
X, Y, data = PiecewiseNormData()
20+
cmap = cm.spectral
21+
22+
# Creating functions for plotting
23+
24+
25+
def makePlot(norm, label=''):
26+
fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw={
27+
'width_ratios': [1, 2]}, figsize=[9, 4.5])
28+
fig.subplots_adjust(top=0.87, left=0.07, right=0.96)
29+
fig.suptitle(label)
30+
31+
cax = ax2.pcolormesh(X, Y, data, cmap=cmap, norm=norm)
32+
ticks = cax.norm.ticks() if norm else None
33+
cbar = fig.colorbar(cax, format='%.3g', ticks=ticks)
34+
ax2.set_xlim(X.min(), X.max())
35+
ax2.set_ylim(Y.min(), Y.max())
36+
37+
data_values = np.linspace(cax.norm.vmin, cax.norm.vmax, 100)
38+
cm_values = cax.norm(data_values)
39+
ax1.plot(data_values, cm_values)
40+
ax1.set_xlabel('Data values')
41+
ax1.set_ylabel('Colormap values')
42+
43+
44+
def make3DPlot(label=''):
45+
fig = plt.figure()
46+
fig.suptitle(label)
47+
ax = fig.gca(projection='3d')
48+
cax = ax.plot_surface(X, Y, data, rstride=1, cstride=1,
49+
cmap=cmap, linewidth=0, antialiased=False)
50+
ax.set_zlim(data.min(), data.max())
51+
fig.colorbar(cax, shrink=0.5, aspect=5)
52+
ax.view_init(20, 225)
53+
54+
55+
# Showing how the data looks in linear scale
56+
make3DPlot('Regular linear scale')
57+
makePlot(None, 'Regular linear scale')
58+
59+
# Example of logarithm normalization using FuncNorm
60+
norm = colors.FuncNorm(f=lambda x: np.log10(x),
61+
finv=lambda x: 10.**(x), vmin=0.01, vmax=2)
62+
makePlot(norm, "Log normalization using FuncNorm")
63+
# The same can be achived with
64+
# norm = colors.FuncNorm(f='log',vmin=0.01,vmax=2)
65+
66+
# Example of root normalization using FuncNorm
67+
norm = colors.FuncNorm(f='sqrt', vmin=0.0, vmax=2)
68+
makePlot(norm, "Root normalization using FuncNorm")
69+
70+
# Performing a symmetric amplification of the features around 0
71+
norm = colors.MirrorPiecewiseNorm(fpos='crt')
72+
makePlot(norm, "Amplified features symetrically around \n"
73+
"0 with MirrorPiecewiseNorm")
74+
75+
76+
# Amplifying features near 0.6 with MirrorPiecewiseNorm
77+
norm = colors.MirrorPiecewiseNorm(fpos='crt', fneg='crt',
78+
center_cm=0.35,
79+
center_data=0.6)
80+
makePlot(norm, "Amplifying positive and negative features\n"
81+
"standing on 0.6 with MirrorPiecewiseNorm")
82+
83+
# Amplifying features near both -0.4 and near 1.2 with PiecewiseNorm
84+
norm = colors.PiecewiseNorm(flist=['cubic', 'crt', 'cubic', 'crt'],
85+
refpoints_cm=[0.25, 0.5, 0.75],
86+
refpoints_data=[-0.4, 1, 1.2])
87+
makePlot(norm, "Amplifying positive and negative features standing\n"
88+
" on -0.4 and 1.2 with PiecewiseNorm")
89+
90+
# Amplifying features near both -1, -0.2 and near 1.2 with PiecewiseNorm
91+
norm = colors.PiecewiseNorm(flist=['crt', 'crt', 'crt'],
92+
refpoints_cm=[0.4, 0.7],
93+
refpoints_data=[-0.2, 1.2])
94+
makePlot(norm, "Amplifying only positive features standing on -1, -0.2\n"
95+
" and 1.2 with PiecewiseNorm")
96+
97+
98+
plt.show()
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
================================================================
3+
Creating sample data for the different examples on normalization
4+
================================================================
5+
6+
Data with special features tailored to the need of the different examples on
7+
colormal normalization is created.
8+
9+
"""
10+
11+
import numpy as np
12+
13+
14+
def PiecewiseNormData(NX=512, NY=256):
15+
"""Sample data for the PiecewiseNorm class.
16+
17+
Returns a 2d array with sample data, along with the X and Y values for the
18+
array.
19+
20+
Parameters
21+
----------
22+
NX : int
23+
Number of samples for the data accross the horizontal dimension.
24+
Default is 512.
25+
NY : int
26+
Number of samples for the data accross the vertical dimension.
27+
Default is 256.
28+
29+
Returns
30+
-------
31+
X, Y, data : ndarray of shape (NX,NY)
32+
Values for the `X` coordinates, the `Y` coordinates, and the `data`.
33+
34+
Examples
35+
--------
36+
>>> X,Y,Z=PiecewiseNormData()
37+
"""
38+
39+
xmax = 16 * np.pi
40+
x = np.linspace(0, xmax, NX)
41+
y = np.linspace(-2, 2, NY)
42+
X, Y = np.meshgrid(x, y)
43+
44+
data = np.zeros(X.shape)
45+
46+
def gauss2d(x, y, a0, x0, y0, wx, wy):
47+
return a0 * np.exp(-(x - x0)**2 / wx**2 - (y - y0)**2 / wy**2)
48+
49+
maskY = (Y > -1) * (Y <= 0)
50+
N = 31
51+
for i in range(N):
52+
maskX = (X > (i * (xmax / N))) * (X <= ((i + 1) * (xmax / N)))
53+
mask = maskX * maskY
54+
data[mask] += gauss2d(X[mask], Y[mask], 2. * i / (N - 1), (i + 0.5) *
55+
(xmax / N), -0.25, xmax / (3 * N), 0.07)
56+
data[mask] -= gauss2d(X[mask], Y[mask], 1. * i / (N - 1), (i + 0.5) *
57+
(xmax / N), -0.75, xmax / (3 * N), 0.07)
58+
59+
maskY = (Y > 0) * (Y <= 1)
60+
data[maskY] = np.cos(X[maskY]) * Y[maskY]**2
61+
62+
N = 61
63+
maskY = (Y > 1) * (Y <= 2.)
64+
for i, val in enumerate(np.linspace(-1, 1, N)):
65+
if val < 0:
66+
aux = val
67+
if val > 0:
68+
aux = val * 2
69+
70+
maskX = (X >= (i * (xmax / N))) * (X <= ((i + 1) * (xmax / N)))
71+
data[maskX * maskY] = aux
72+
73+
N = 11
74+
maskY = (Y <= -1)
75+
for i, val in enumerate(np.linspace(-1, 1, N)):
76+
if val < 0:
77+
factor = 1
78+
if val >= 0:
79+
factor = 2
80+
maskX = (X >= (i * (xmax / N))) * (X <= ((i + 1) * (xmax / N)))
81+
mask = maskX * maskY
82+
data[mask] = val * factor
83+
84+
if i != N - 1:
85+
data[mask] += gauss2d(X[mask], Y[mask], 0.05 * factor, (i + 0.5) *
86+
(xmax / N), -1.25, xmax / (3 * N), 0.07)
87+
if i != 0:
88+
data[mask] -= gauss2d(X[mask], Y[mask], 0.05 * factor, (i + 0.5) *
89+
(xmax / N), -1.75, xmax / (3 * N), 0.07)
90+
return X, Y, data

0 commit comments

Comments
 (0)