0

I try to run this python code but it's giving error. I wanted to create a stacked 3d map.

`import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D #<-- Note the capitalization! 

fig    = plt.figure()
#ax     = fig.gca(projection='3d')
ax = Axes3D(fig)

x      = np.linspace(0, 1, 100)
X, Y   = np.meshgrid(x, x)
Z1 = .1*np.sin(2*X)*np.sin(4*Y)
Z2 = .1*np.sin(3*X)*np.sin(4*Y)
Z3 = .1*np.sin(4*X)*np.sin(5*Y)

levels=np.linspace(Z1.min(), Z1.max(), 100)
ax.contourf(X, Y,Z1, levels=levels, zdir='z', offset=0, cmap=plt.get_cmap('rainbow'))

levels=np.linspace(Z2.min(), Z2.max(), 100)
ax.contourf(X, Y,Z2, levels=levels, zdir='z', offset=1, cmap=plt.get_cmap('rainbow'))

levels=np.linspace(Z3.min(), Z3.max(), 100)
ax.contourf(X, Y,Z3, levels=levels, zdir='z', offset=2, cmap=plt.get_cmap('rainbow'))

ax.set_xlim3d(0, 1)
ax.set_ylim3d(0, 1)
ax.set_zlim3d(0, 2)

plt.show() `

And I was getting this error: TypeError: gca() got an unexpected keyword argument 'projection' Please, can anyone resolve this issue for me?

TThoye
  • 25
  • 5
  • You seem to be following an example made by an old matplotlib version. Maybe you can try `ax = fig.add_subplot(projection='3d')`? Or just leave out that line. See matplotlib's [current examples](https://matplotlib.org/stable/gallery/mplot3d/index.html). – JohanC Jan 24 '23 at 21:34

0 Answers0