Trying to add a 3d subplot to a matplotlib figure

Dane Allen picture Dane Allen · Dec 14, 2011 · Viewed 16.3k times · Source

So I'm trying to create a figure that presents a 3d plot from data points, along with the plots 3 projections in 3 other subplots. I can add the subplots for the projections with no problems, but when I try to place the 3 dimensional plot into the figure things backfire.

here's my code:

def plotAll(data):
    fig = plt.figure()
    plot_3d = fig.add_subplot(221)
    ax = Axes3D(plot_3d)  
    for i,traj in enumerate(data.values()):
        ax.plot3D([traj[0][-1]],[traj[1][-1]],[traj[2][-1]],".",color=[0.91,0.39,0.046])    
    #plot_12v13 = fig.add_subplot(222)
    #plot_projections(data,0,1)
    #plot_13v14 = fig.add_subplot(223)
    #plot_projections(data,1,2)
    #plot_12v14 = fig.add_subplot(224)
    #plot_projections(data,0,2)
    #plt.plot()

which throws back: 'AxesSubplot' object has no attribute 'transFigure'

I'm using matplotlib 0.99.3, any help would be greatly appreciated, thanks!

Answer

Xidus picture Xidus · Jan 13, 2014

I was searching for a way to create my 3D-plots with the nice fig, axes = plt.subplots(...) shortcut, but since I just browsed Matplotlib's mplot3d tutorial, I want to share a quote from the top of this site.

New in version 1.0.0: This approach is the preferred method of creating a 3D axes.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

Note

Prior to version 1.0.0, the method of creating a 3D axes was different. For those using older versions of matplotlib, change ax = fig.add_subplot(111, projection='3d') to ax = Axes3D(fig).

So if you have to use the <1.0.0 version of Matplotlib, this should be taken into account.