I am plotting a function z
that is generated from a MWA like this:
import numpy as np
import matplotlib.pyplot as plt
ylist = np.linspace(0,10,20)
xlist = np.linspace(0,10,20)
zR = [] # real part of z
zI = [] # imaginary part of z
for y in ylist:
for x in xlist:
z = 0.2*x**2 + 1j*(x*y+0.7*np.sqrt(y))
zR.append(z.real)
zI.append(z.imag)
zR = np.array(zR)
z_matrixR = zR.reshape((ylist.size, xlist.size))
zI = np.array(zI)
z_matrixI = zI.reshape((ylist.size, xlist.size))
fig = plt.figure(figsize=(8.0,6.0))
ax1 = fig.add_subplot(221)
z_contourR = ax1.contourf(xlist,ylist,z_matrixR,100)
plt.xlabel('x')
plt.ylabel('y')
plt.title('real')
#cbarR = plt.colorbar(z_contourR)
ax2 = fig.add_subplot(222)
z_contourI = ax2.contourf(xlist,ylist,z_matrixI,100)
plt.xlabel('x')
plt.ylabel('y')
plt.title('imaginary')
#cbarI = plt.colorbar(z_contourI)
plt.tight_layout()
plt.show()
I want both the real and imaginary parts of my z
function to share the same colorbar, but I'm not sure how to do this. I have looked at this and this example, but am still unsure about how to apply it to my code.