Friday, January 15, 2016

Plot Map Customize Colorscale


#!/usr/bin/python
import numpy as np
import scipy
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import matplotlib.mlab as ml
import scipy.ndimage as ndimage
from matplotlib import mlab, cm

def extrapolate_nans(x, y, v):
if np.ma.is_masked(v):
nans = v.mask
else:
nans = np.isnan(v)
notnans = np.logical_not(nans)
v[nans] = scipy.interpolate.griddata((x[notnans], y[notnans]), v[notnans],
(x[nans], y[nans]), method='nearest').ravel()
return v


data = np.genfromtxt('input2d.txt')
x1 = data[:,0]
y1 = data[:,1]
z1 = data[:,2]

numcols, numrows = 500, 500
xi = np.linspace(min(x1), max(x1), numcols)
yi = np.linspace(min(y1), max(y1), numrows)
xi, yi = np.meshgrid(xi, yi)
x, y, z = x1, y1, z1
zi = ml.griddata(x, y, z, xi, yi, interp='nn')
extrapolate_nans(xi,yi,zi)

'''

cmaps = [('Perceptually Uniform Sequential',
['viridis', 'inferno', 'plasma', 'magma']),
('Sequential', ['Blues', 'BuGn', 'BuPu',
'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd',
'PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu',
'Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']),
('Sequential (2)', ['afmhot', 'autumn', 'bone', 'cool',
'copper', 'gist_heat', 'gray', 'hot',
'pink', 'spring', 'summer', 'winter']),
('Diverging', ['BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn', 'PuOr',
'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'Spectral',
'seismic']),
('Qualitative', ['Accent', 'Dark2', 'Paired', 'Pastel1',
'Pastel2', 'Set1', 'Set2', 'Set3']),
('Miscellaneous', ['gist_earth', 'terrain', 'ocean', 'gist_stern',
'brg', 'CMRmap', 'cubehelix',
'gnuplot', 'gnuplot2', 'gist_ncar',
'nipy_spectral', 'jet', 'rainbow',
'gist_rainbow', 'hsv', 'flag', 'prism'])]
'''

levels = np.arange(-1200, -200, 25) # Boost the upper limit to avoid truncation errors.
norm = cm.colors.Normalize(vmax=abs(z1).max(), vmin=-abs(z1).max())
cmap = cm.hsv



####plot
zis = ndimage.gaussian_filter(zi, sigma=25.0, order=0)
fig=plt.figure()
ax=fig.add_subplot(1,2,1)
plt.contour(xi, yi, zi, 20, linewidths = 0.5, colors = 'k')
plt.contourf(xi, yi, zi, levels, cmap=cm.get_cmap(cmap, len(levels) - 1),norm=norm, )


ax=fig.add_subplot(1,2,2)
plt.contour(xi, yi, zis, 20, linewidths = 0.5, colors = 'k')
plt.contourf(xi, yi, zis, levels, cmap=cm.get_cmap(cmap, len(levels) - 1),norm=norm, )
plt.colorbar()

plt.show()

No comments: