Pcolor with cut data below lower limit using Python and matplotlib.pyplot


The result is:

This page shows how to generate pcolor graph with a data whose small values below minimum limit are cut using python and matplotlib.

This page shows how to generate pcolor graph with a data whose small values below minimum limit are cut using python and matplotlib.

See also:

Python Matplotlib Tips: Kernel density estimation using Python, matplotlib.pyplot and scipy.stats.gaussian_kde

This page shows how to change the color of the scatter point according to the density of the surrounding points using python and scipy.stats.gaussian_kde and matplotlib.


In [1]:
import platform
print('python: '+platform.python_version())
import numpy as np
print('numpy: '+np.__version__)
import matplotlib
print('matplotlib: '+matplotlib.__version__)
import matplotlib.pyplot as plt
python: 3.6.3
numpy: 1.15.4
matplotlib: 3.0.1
In [2]:
nx = 201
ny = 121
x = np.linspace(-5,5,nx)
y = np.linspace(-3,3,ny)
xv, yv = np.meshgrid(x, y)


# use 2D normal distribution as sample data
def norm2d(x,y,sigma,xc=0,yc=0):
    Z = np.exp(-((x-xc)**2 + (y-yc)**2) / (2 * sigma**2)) / (2 * np.pi * sigma**2)
    return Z

# sample data is the sum of two 2D normal distributions and random value
z = norm2d(xv,yv,1) + norm2d(xv,yv,1.2,xc=3,yc=0) + np.random.random((ny,nx))*0.1

check the shape and data (only around head)

In [3]:
x.shape,y.shape,z.shape,x[:10],y[:10],z[:10,:10]
Out[3]:
((201,),
 (121,),
 (121, 201),
 array([-5.  , -4.95, -4.9 , -4.85, -4.8 , -4.75, -4.7 , -4.65, -4.6 ,
        -4.55]),
 array([-3.  , -2.95, -2.9 , -2.85, -2.8 , -2.75, -2.7 , -2.65, -2.6 ,
        -2.55]),
 array([[0.09376596, 0.01172964, 0.01828922, 0.07891724, 0.05700937,
         0.02148789, 0.05799124, 0.09697929, 0.00027321, 0.05864659],
        [0.00521733, 0.01072767, 0.09509202, 0.01875523, 0.05784239,
         0.08890037, 0.0125312 , 0.00191997, 0.01980983, 0.00279513],
        [0.08756482, 0.0997649 , 0.05705382, 0.08873478, 0.01787802,
         0.01669059, 0.08793025, 0.04433874, 0.01029348, 0.06492438],
        [0.04012313, 0.02445122, 0.0201331 , 0.02906575, 0.06095721,
         0.07819297, 0.04223324, 0.04780921, 0.07950739, 0.07997944],
        [0.00442169, 0.01527475, 0.00558656, 0.03704475, 0.09333214,
         0.04963186, 0.09029644, 0.04623457, 0.00290525, 0.02758671],
        [0.00819027, 0.03271065, 0.01549958, 0.03793254, 0.01937213,
         0.02918789, 0.03459305, 0.01303321, 0.05126489, 0.05232104],
        [0.06410652, 0.01769397, 0.05703856, 0.0823152 , 0.08640173,
         0.06728535, 0.04091868, 0.03824223, 0.04246348, 0.06742947],
        [0.09796439, 0.04164535, 0.01924326, 0.02691657, 0.09399129,
         0.02532103, 0.0869668 , 0.03094627, 0.0821657 , 0.04303998],
        [0.04180734, 0.0558714 , 0.0090016 , 0.01665698, 0.03588597,
         0.05457192, 0.07574392, 0.05803575, 0.00117392, 0.08517043],
        [0.07579318, 0.05364112, 0.09216873, 0.01063778, 0.00325895,
         0.06951875, 0.0866892 , 0.03909408, 0.05748661, 0.05533672]]))

plot data normally

In [4]:
# decide upper and lower limit
ulim = 0.25 # upper limit
llim = 0.1  # lower limit
zplot = z.copy()
zplot[np.where(zplot>ulim)] = ulim
zplot[np.where(zplot<llim)] = llim


# convert to figure
plt.figure(figsize=(6,5))
plt.pcolor(x,y,zplot,cmap='jet')
# generate a colorbar
cbar = plt.colorbar()
ctks = np.linspace(llim,ulim,4)
cbar.set_ticks(ctks)
cbar.set_ticklabels(ctks)
# set x,y, and c label
plt.xlabel('x label')
plt.ylabel('y label')
cbar.set_label('c label')

plt.savefig('pcolor_low_cut_1.png', bbox_inches='tight', pad_inches=0.05, dpi=150)

hide data that are below the lower limit

In [5]:
# decide upper and lower limit
ulim = 0.25 # upper limit
llim = 0.1  # lower limit
zplot = z.copy()
zplot[np.where(zplot>ulim)] = ulim
zplot[np.where(zplot<llim)] = np.nan # if use np.nan, the data are not displayed in the gigure


# convert to figure
plt.figure(figsize=(6,5))
plt.pcolor(x,y,zplot,cmap='jet')
# generate a colorbar
cbar = plt.colorbar()
ctks = np.linspace(llim,ulim,4)
cbar.set_ticks(ctks)
cbar.set_ticklabels(ctks)
# set x,y, and c label
plt.xlabel('x label')
plt.ylabel('y label')
cbar.set_label('c label')

plt.savefig('pcolor_low_cut_2.png', bbox_inches='tight', pad_inches=0.05, dpi=150)

try another colormap

In [6]:
# decide upper and lower limit
ulim = 0.25 # upper limit
llim = 0.1  # lower limit
zplot = z.copy()
zplot[np.where(zplot>ulim)] = ulim
zplot[np.where(zplot<llim)] = np.nan # if use np.nan, the data are not displayed in the gigure


# convert to figure
plt.figure(figsize=(6,5))
plt.pcolor(x,y,zplot,cmap='hot_r')
# generate a colorbar
cbar = plt.colorbar()
ctks = np.linspace(llim,ulim,4)
cbar.set_ticks(ctks)
cbar.set_ticklabels(ctks)
# set x,y, and c label
plt.xlabel('x label')
plt.ylabel('y label')
cbar.set_label('c label')

plt.savefig('pcolor_low_cut_3.png', bbox_inches='tight', pad_inches=0.05, dpi=150)