|
| 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() |
0 commit comments