Skip to content

Instantly share code, notes, and snippets.

@ahmedshaaban1
Created January 18, 2020 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedshaaban1/1259e1efc7d8aeb843c2440511a22658 to your computer and use it in GitHub Desktop.
Save ahmedshaaban1/1259e1efc7d8aeb843c2440511a22658 to your computer and use it in GitHub Desktop.
Extending the white color in the middle of blue-white-red colormap.
import numpy as np
import matplotlib.colors
cmap=plt.cm.bwr
cmap1=[cmap(i) for i in np.arange(cmap.N)]
cen1=int(cmap.N/2)
cen2=int(cen1-1)
for i in np.arange(cen2-3,cen1+4):
print(i)
cmap1[i]=(1.0,1.0,1.0,1.0)
cmap2=matplotlib.colors.LinearSegmentedColormap.from_list('my_cmap',cmap1,cmap.N)
for i in np.arange(256):
print(str(i)+' '+str(cmap1[i]))
x1=np.linspace(0,1,256)
yyy=np.vstack((x1,x1,x1,x1,x1,x1,x1,x1,x1,x1,x1,x1))
fig,ax=plt.subplots(1,1);ax.imshow(yyy,cmap=cmap2)
ax.set_xticks((0,100,126,127,256));plt.xticks(rotation=90)
plt.show()
#% example
N = 100
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
plt.figure();plt.contourf(Z,np.arange(-2,2.1,0.1),cmap=cmap2);plt.colorbar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment