7

I've created some axes using plt.subplots(nrows = 2, ncols = 2). I would like to make ax[1,1] a 3d axis so I can make a plot like this.

Is there anyway I can turn an existing ax object into a 3d ax?

Demetri Pananos
  • 6,770
  • 9
  • 42
  • 73

1 Answers1

13

No, because a 3D axis is a matplotlib.axes._subplots.Axes3DSubplot object, while a regular axis is a matplotlib.axes._subplots.AxesSubplot object.

So, its not just the case of changing one property of an existing object, since its a completely different object that gets created when you add_subplot(projection='3d').

I think you would have to explicitly create your subplots, something like:

fig=plt.figure()
ax1=fig.add_subplot(2,2,1)
ax2=fig.add_subplot(2,2,2)
ax3=fig.add_subplot(2,2,3)
ax4=fig.add_subplot(2,2,4,projection='3d')

Or, alternatively, remove the 2D axis and add it back in as a 3D axis:

fig,ax = plt.subplots(nrows = 2, ncols = 2)
ax[1,1].remove()
ax[1,1]=fig.add_subplot(2,2,4,projection='3d')
tmdavison
  • 64,360
  • 12
  • 187
  • 165