Search code examples
pythonmatplotlibmatplotlib-3d

Adding errorbars to 3D plot


I can't find a way to draw errorbars in a 3D scatter plot in matplotlib. Basically, for the following piece of code

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(1)
ax.scatter(X, Y, zs = Z, zdir = 'z')

I am looking for something like

ax.errorbar(X,Y, zs = Z, dY, dX, zserr = dZ)

Is there a way to do this in mplot3d? If not, are there other libraries with this function?


Solution

  • I ended up writing the method for matplotlib: official example for 3D errorbars:

    enter image description here

    import matplotlib.pyplot as plt
    import numpy as np
    
    ax = plt.figure().add_subplot(projection='3d')
    
    # setting up a parametric curve
    t = np.arange(0, 2*np.pi+.1, 0.01)
    x, y, z = np.sin(t), np.cos(3*t), np.sin(5*t)
    
    estep = 15
    i = np.arange(t.size)
    zuplims = (i % estep == 0) & (i // estep % 3 == 0)
    zlolims = (i % estep == 0) & (i // estep % 3 == 2)
    
    ax.errorbar(x, y, z, 0.2, zuplims=zuplims, zlolims=zlolims, errorevery=estep)
    
    ax.set_xlabel("X label")
    ax.set_ylabel("Y label")
    ax.set_zlabel("Z label")
    
    plt.show()