Processing example 1: Colormaps in 2d Data

Step 0: Setup environment


In [1]:
%matplotlib inline
import matplotlib
import seaborn as sns
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
%cd C:\Users\da.angulo39\Documents\ipython_notebooks\Assignment1\Example1\data

#figure size
matplotlib.rcParams['figure.figsize'] = (10.0, 8.0)


C:\Users\da.angulo39\Documents\ipython_notebooks\Assignment1\Example1\data

Step 1: Load the data


In [12]:
vortex_file = "temperature.asc"
in_file = open(vortex_file)
#first line contains dimension and spacing of grid
l = in_file.readline().split()
nx, ny = int(l[0]), int(l[1])
l = in_file.readline().split()
sx, sy = float(l[0]), float(l[1])

In [13]:
#initialize data object for speed
data = np.zeros(ny*nx)
start = 0

In [14]:
for l in iter(in_file.readline,""):
    values = [float(x) for x in l.split()]
    data[start:start+len(values)] = values
    start += len(values)
in_file.close()
data = data.reshape((ny,nx))

In [66]:
print data[5,5]


0.0026946405

Step 2: Display as image

We can simply use the imshow method


In [24]:
plt.imshow(data,aspect=sy/sx)


Out[24]:
<matplotlib.image.AxesImage at 0x16e306a0>

Step 3: Use colormaps

We are going to use some of the colormaps defined in colorbrewer via seaborn

We will use 8 colors to illustrate the palettes


In [16]:
grayscale = sns.color_palette("Greys",8)
sns.palplot(grayscale)



In [17]:
hot = sns.color_palette("YlOrRd",8)
sns.palplot(hot)



In [37]:
hotcold = sns.color_palette("RdBu",8)
hotcold.reverse()
sns.palplot(hotcold)



In [34]:
rainbow = sns.color_palette("husl",8)
sns.palplot(rainbow)



In [20]:
plt.imshow(data,cmap=sns.blend_palette(grayscale,as_cmap=True),aspect=sy/sx)


Out[20]:
<matplotlib.image.AxesImage at 0x162b9cf8>

In [21]:
plt.imshow(data,cmap=sns.blend_palette(hot,as_cmap=True),aspect=sy/sx)


Out[21]:
<matplotlib.image.AxesImage at 0x1653acf8>

In [38]:
plt.imshow(data,cmap=sns.blend_palette(hotcold,as_cmap=True),aspect=sy/sx)


Out[38]:
<matplotlib.image.AxesImage at 0x17df57b8>

In [52]:
plt.imshow(data,cmap=sns.blend_palette(rainbow,as_cmap=True),aspect=sy/sx)


Out[52]:
<matplotlib.image.AxesImage at 0x1a6b5ef0>

In [51]:
#lets add contour lines
plt.figure()
plt.hold(True)
im = plt.imshow(data,cmap=sns.blend_palette(hotcold,as_cmap=True))
cs = plt.contour(data)
plt.clabel(cs)
CBI = plt.colorbar(im, orientation='vertical', shrink=0.8)



In [ ]: