In [1]:
import sys, os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors as mc
%matplotlib inline
In [2]:
palette = [
"#000000", "#0019ff", "#0080ff",
"#00e5ff", "#00ff4d", "#19fe00",
"#80ff00", "#e6ff00", "#ffff00",
"#ffe53c", "#ffdb77", "#ffe0b2",
]
n = len(palette)
In [3]:
cmap = mc.ListedColormap(palette, 'indexed')
im = plt.imshow([range(n)], interpolation='none', cmap=cmap)
plt.show()
In [4]:
class Raster(np.ndarray):
def __new__(cls, input_array, cellsize, xllcorner, yllcorner):
obj = np.asarray(input_array).view(cls)
obj.cellsize = cellsize
obj.xllcorner = xllcorner
obj.yllcorner = yllcorner
# 1-D list of the raster's values
v = obj.flatten()
obj.values = v[~np.isnan(v)]
return obj
@classmethod
def load(cls, filename):
data = [ ]
attrs = { }
with open(filename) as f:
for i in range(6):
key,value = next(f).strip().split()
attrs[key] = value
NODATA_value = attrs.get("NODATA_value", "-9999")
for line in f:
row = list(map(lambda x: np.nan if x == NODATA_value else float(x), line.split()))
data.append(row)
return Raster(np.asarray(data), float(attrs["cellsize"])
, float(attrs["xllcorner"])
, float(attrs["yllcorner"]))
In [5]:
%%time
I = Raster.load("../bb450_final.asc")
In [6]:
lo = float(np.min(I.values))
hi = float(np.max(I.values))
mu = float(np.mean(I.values))
sigma = float(np.std(I.values))
In [7]:
print("""
Size: {} x {}
Cell size: {}
Min Value: {}
Max Value: {}
Mean Value: {}
Std Dev: {}
Colors: {}
""".format(I.shape[0], I.shape[1], I.cellsize, lo, hi, mu, sigma, n))
In [8]:
def plot_histogram(I, divs):
# print(list(zip(divs, palette)))
cmap = mc.LinearSegmentedColormap.from_list("", list(zip(divs, palette)))
nbars = 300
width = (hi - lo) / nbars
h,x = np.histogram(I.values - width/2., nbars) # shift the bars slightly, so they are centered over x coord
colors = list(map(cmap, x[1:] / hi)) # data values must be [0.,1.]
plt.figure(figsize=(12, 5))
ax = plt.bar(x[1:], h, width, color=colors, edgecolor=colors)
for i in divs:
plt.axvline(x=i*hi, linestyle="--", color="#cecece")
plt.yscale("symlog")
plt.ylabel("Count")
plt.xlabel("Amps")
plt.xlim([-0.1,None])
In [9]:
def plot_map(I, divs):
cmap = mc.LinearSegmentedColormap.from_list("", list(zip(divs, palette)))
fig = plt.figure(figsize=(10,10))
img = plt.imshow(I, origin="upper", cmap=cmap)
fig.colorbar(img)
In [10]:
divs = [ i/(n-1.) for i in range(n) ]
divs
Out[10]:
In [11]:
plot_histogram(I, divs)
In [12]:
plot_map(I, divs)
No idea what's an appropriate values for c is. It depends on the map at hand. In the gview source
code c=2 was hard coded.
In [13]:
c = 1.75
divs = [0.] + [ 1/c**(n-i-1) for i in range(1,n) ]
divs
Out[13]:
In [14]:
plot_histogram(I, divs)
In [15]:
plot_map(I, divs)
In [16]:
divs = [0.] + [ (i*sigma+mu)/hi for i in range(1,n-1) ] + [1.]
divs
Out[16]:
In [17]:
plot_histogram(I, divs)
In [18]:
plot_map(I, divs)
In [19]:
divs = [0.] + [ a[-1]/hi for a in np.array_split(np.sort(I.values), n-1) ]
divs
Out[19]:
In [20]:
plot_histogram(I, divs)
In [21]:
plot_map(I, divs)
In [ ]: