1

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.

Community
  • 1
  • 1
Medulla Oblongata
  • 3,771
  • 8
  • 36
  • 75

1 Answers1

3

vmin, vmax and levels are your friends. 3 options you may want:

use the same ones when plotting both real and imaginary parts

fig = plt.figure(figsize=(8.0,6.0))
ax1 = fig.add_subplot(221)    

vmax=30
vmin=0
levels = np.linspace(vmin,vmax,100)

z_contourR = ax1.contourf(xlist,ylist,z_matrixR,levels=levels,vmax=vmax,vmin=vmin)
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,levels=levels,vmax=vmax,vmin=vmin)
plt.xlabel('x')
plt.ylabel('y')
plt.title('imaginary')
cbarI = plt.colorbar(z_contourI)

plt.tight_layout()
plt.show()

enter image description here

the top right is white since it's out of the the levels I specified, you can get it back in by choosing a different maximum for levels and vmax this would create a funny looking colorbar but you might want it in some cases

fig = plt.figure(figsize=(8.0,6.0))
ax1 = fig.add_subplot(221)    

vmax=30
levels_max=100
vmin=0
levels = np.arange(vmin,levels_max,.1)

z_contourR = ax1.contourf(xlist,ylist,z_matrixR,levels=levels,vmax=vmax,vmin=vmin)
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,levels=levels,vmax=vmax,vmin=vmin)
plt.xlabel('x')
plt.ylabel('y')
plt.title('imaginary')
cbarI = plt.colorbar(z_contourI)

plt.tight_layout()
plt.show()

enter image description here

now, if you want both the top right filled and the color bar looking right you should redefine levels between drawings and plot both with the first color bar

fig = plt.figure(figsize=(8.0,6.0))
ax1 = fig.add_subplot(221)    

vmax=30
levels_max=30
vmin=0
levels = np.linspace(vmin,levels_max,100)

z_contourR = ax1.contourf(xlist,ylist,z_matrixR,levels=levels,vmax=vmax,vmin=vmin)
plt.xlabel('x')
plt.ylabel('y')
plt.title('real')
cbarR = plt.colorbar(z_contourR)

vmax=30
levels_max=100
vmin=0
levels = np.linspace(vmin,levels_max,100)


ax2 = fig.add_subplot(222)
z_contourI = ax2.contourf(xlist,ylist,z_matrixI,levels=levels,vmax=vmax,vmin=vmin)
plt.xlabel('x')
plt.ylabel('y')
plt.title('imaginary')
cbarI = plt.colorbar(z_contourR)

plt.tight_layout()
plt.show()

enter image description here

OMRY VOLK
  • 1,429
  • 11
  • 24