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.
See also:
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
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]:
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)