Processing example 1: Colormaps in 2d Data

Step 0: Setup environment


In [62]:
%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 [63]:
vortex_file = "vorticity.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])
sx, sy = float(l[2]), float(l[3])

In [64]:
#initialize data object for speed
data = np.zeros((ny,nx))

In [65]:
for j in xrange(ny):
    l = in_file.readline().split()
    values = [float(x) for x in l]
    data[j,:] = values
in_file.close()

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


0.0026946405

Step 2: Display as image

We can simply use the imshow method


In [67]:
plt.imshow(data)


Out[67]:
<matplotlib.image.AxesImage at 0x1770cc18>

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 [68]:
grayscale = sns.color_palette("Greys",8)
sns.palplot(grayscale)



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



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



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



In [72]:
plt.imshow(data,cmap=sns.blend_palette(grayscale,as_cmap=True))


Out[72]:
<matplotlib.image.AxesImage at 0x1943eda0>

In [73]:
plt.imshow(data,cmap=sns.blend_palette(hot,as_cmap=True))


Out[73]:
<matplotlib.image.AxesImage at 0x1981abe0>

In [74]:
plt.imshow(data,cmap=sns.blend_palette(hotcold,as_cmap=True))


Out[74]:
<matplotlib.image.AxesImage at 0x19b79a58>

In [75]:
plt.imshow(data,cmap=sns.blend_palette(rainbow,as_cmap=True))


Out[75]:
<matplotlib.image.AxesImage at 0x191dfb00>

In [75]: