Lid driven Cavity (GPU)

The following command is important to view matplotlib plots on a jupyter notebook


In [1]:
# %matplotlib inline

In [2]:
%matplotlib notebook

In [3]:
import matplotlib.pyplot as plt

In [4]:
from mpl_toolkits.mplot3d import Axes3D

In [5]:
import os, sys

In [6]:
from matplotlib.mlab import griddata

In [7]:
import numpy as np

Making a colorbar, making colormaps, Show colormaps, in matplotlib

making your own color bar


In [8]:
from pylab import *

In [9]:
cdict = {'red': ((0.0, 0.0, 0.0),
                (0.5, 1.0, 0.7),
                (1.0, 1.0, 1.0)), 
        'green': ((0.0, 0.0, 0.0), 
                 (0.5, 1.0, 0.0), 
                 (1.0, 1.0, 1.0)), 
        'blue': ((0.0, 0.0, 0.0),
                (0.5, 1.0, 0.0),
                (1.0, 0.5, 1.0))}

In [10]:
my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',cdict,256)

In [11]:
pcolor(rand(10,10),cmap=my_cmap)
colorbar()


Out[11]:
<matplotlib.colorbar.Colorbar at 0x7f806fa9c9d0>

where rand(10,10) is a 10x10 array of random numbers in the range $[0.0, 1.0)$


In [12]:
rand(10,10)


Out[12]:
array([[  6.86040949e-01,   9.40534792e-01,   4.07140143e-01,
          7.24149635e-01,   8.58142152e-01,   2.15366347e-01,
          1.62501280e-01,   2.79871073e-02,   5.70201696e-01,
          5.16922643e-01],
       [  3.28618105e-01,   8.23557984e-01,   1.38426477e-01,
          4.12206837e-01,   7.66773809e-01,   8.10581687e-01,
          7.70794962e-01,   6.81793841e-01,   7.10979123e-01,
          9.85421819e-01],
       [  5.78253553e-01,   6.85361073e-01,   8.50571299e-01,
          5.83388511e-02,   9.31269290e-01,   2.18212096e-01,
          8.41934963e-01,   2.52056282e-01,   1.69788014e-02,
          7.40342657e-01],
       [  7.60113363e-01,   1.42457676e-01,   1.84276674e-01,
          2.47925556e-02,   9.74570329e-01,   9.97785816e-01,
          3.75570544e-02,   7.70097701e-01,   1.89902085e-01,
          9.92733616e-01],
       [  3.74047453e-01,   2.05289745e-01,   4.09023361e-01,
          3.59591353e-01,   9.50900180e-01,   8.84992111e-01,
          7.44247438e-01,   1.69140504e-01,   3.99182770e-01,
          6.72070963e-01],
       [  6.17543085e-01,   9.56834693e-01,   4.18427264e-03,
          1.10711528e-01,   8.67890317e-01,   4.98372421e-01,
          9.88075132e-01,   5.74503571e-01,   3.77224668e-01,
          7.70599414e-01],
       [  8.80343744e-01,   1.01945757e-01,   5.20618099e-01,
          4.63148736e-02,   6.04976744e-01,   5.10370295e-01,
          9.99115303e-01,   3.75310607e-01,   6.52179833e-01,
          9.09434815e-01],
       [  4.86164288e-01,   6.87958231e-02,   2.75881125e-01,
          4.08826319e-01,   6.84275156e-02,   9.29418403e-01,
          7.12997161e-01,   4.90900926e-01,   9.96694647e-01,
          4.08542524e-01],
       [  3.70974996e-01,   9.49813413e-01,   5.65984556e-01,
          2.56332045e-02,   3.44499559e-01,   2.33830903e-01,
          2.43790842e-01,   3.69365003e-01,   5.97539334e-01,
          4.11915080e-01],
       [  1.34723394e-05,   8.07703134e-01,   3.89890405e-01,
          9.91399578e-01,   5.31016091e-01,   2.62716700e-01,
          8.76652796e-01,   4.26818089e-02,   6.20065162e-01,
          3.21951082e-01]])

In [13]:
cdict2 = {'red': ((0.0, 0.0, 0.0), 
                    (0.3, 0.5, 0.5),
                     (0.6, 0.7, 0.7),
                     (0.9, 0.8, 0.8),
                     (1.0, 0.8, 0.8)), 
        'green': ((0.0, 0.0, 0.0), 
                (0.3, 0.8, 0.8),
                (0.6, 0.7, 0.7),
                 (0.9, 0.0, 0.0),
                 (1.0, 0.7, 0.7)),
         'blue': ((0.0, 1.0, 1.0),
                 (0.3, 1.0, 1.0),
                 (0.6, 0.0, 0.0),
                 (0.9, 0.0, 0.0),
                 (1.0, 1.0, 1.0))}

In [14]:
cmap1 = matplotlib.colors.LinearSegmentedColormap('my_colormap2', cdict2, N=256 )

In [15]:
cmap2 = matplotlib.colors.LinearSegmentedColormap('my_colormap2', cdict2, N=256, gamma=0.75 )

In [16]:
pcolor(rand(10,10),cmap=cmap1 )
colorbar()


Out[16]:
<matplotlib.colorbar.Colorbar at 0x7f806d5a1090>

In [17]:
pcolor(rand(10,10),cmap=cmap2 )
colorbar()


Out[17]:
<matplotlib.colorbar.Colorbar at 0x7f806ccd99d0>

In [39]:
cdict3 = {'red': ((0.0, 1.0, 1.0),
                 (0.2,1.0, 1.0 ),
                 (0.4,0.0,0.0),
                 (0.6,0.0,0.0),
                 (0.8,0.0,0.0),
                 (1.0,1.0,1.0)), 
         'green': ((0.0,0.0,0.0),
                  (0.2,1.0,1.0),
                  (0.4,1.0,1.0),
                  (0.6,1.0,1.0),
                  (0.8,0.0, 0.0),
                  (1.0,0.0,0.0)),
         'blue': ((0.0,0.0,0.0),
                 (0.2,0.0,0.0),
                 (0.4,0.0,0.0),
                 (0.6,1.0,1.0),
                 (0.8,1.0,1.0),
                 (1.0,1.0,1.0))}

In [35]:
cmap3 = matplotlib.colors.LinearSegmentedColormap('my_colormap3', cdict3, N=256)

In [36]:
pcolor(rand(10,10),cmap=cmap3)
colorbar()


Out[36]:
<matplotlib.colorbar.Colorbar at 0x7f806c8d4090>

In [22]:
minval = -0.4
maxval = 1.6
rangeval = maxval - minval

In [26]:
rangeval*0.2


Out[26]:
0.4

In [27]:
"""
cdict3b = {'red': ((minval + rangeval*0.2*0, 1.0, 1.0),
                 (minval + rangeval*0.2*1,1.0, 1.0 ),
                 (minval + rangeval*0.2*2,0.0,0.0),
                 (minval + rangeval*0.2*3,0.0,0.0),
                 (minval + rangeval*0.2*4,1.0,0.0),
                 (minval + rangeval*0.2*5,1.0,1.0)), 
         'green': ((minval + rangeval*0.2*0,0.0,0.0),
                  (minval + rangeval*0.2*1,1.0,1.0),
                  (minval + rangeval*0.2*2,1.0,1.0),
                  (minval + rangeval*0.2*3,1.0,1.0),
                  (minval + rangeval*0.2*4,0.0, 0.0),
                  (minval + rangeval*0.2*5,0.0,0.0)),
         'blue': ((minval + rangeval*0.2*0,0.0,0.0),
                 (minval + rangeval*0.2*1,0.0,0.0),
                 (minval + rangeval*0.2*2,0.0,0.0),
                 (minval + rangeval*0.2*3,0.0,1.0),
                 (minval + rangeval*0.2*4,1.0,1.0),
                 (minval + rangeval*0.2*5,1.0,1.0))}
                 """

In [28]:
cdict3b


Out[28]:
{'blue': ((-0.4, 0.0, 0.0),
  (0.0, 0.0, 0.0),
  (0.4, 0.0, 0.0),
  (0.8000000000000002, 0.0, 1.0),
  (1.2000000000000002, 1.0, 1.0),
  (1.6, 1.0, 1.0)),
 'green': ((-0.4, 0.0, 0.0),
  (0.0, 1.0, 1.0),
  (0.4, 1.0, 1.0),
  (0.8000000000000002, 1.0, 1.0),
  (1.2000000000000002, 0.0, 0.0),
  (1.6, 0.0, 0.0)),
 'red': ((-0.4, 1.0, 1.0),
  (0.0, 1.0, 1.0),
  (0.4, 0.0, 0.0),
  (0.8000000000000002, 0.0, 0.0),
  (1.2000000000000002, 1.0, 0.0),
  (1.6, 1.0, 1.0))}

In [40]:
cmap3b = matplotlib.colors.LinearSegmentedColormap('my_colormap3b', cdict3, N=256)

In [41]:
pcolor( np.random.uniform(minval, maxval, size=(10,10)),cmap=cmap3b)
colorbar()


Out[41]:
<matplotlib.colorbar.Colorbar at 0x7f806c4432d0>
  • So this would be the color bar or color map relating color to the magnitude of the $x$-component of th