10

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?

3
  • I think you'd have to draw little lines around each point yourself, or try to get the 3D quiver to draw the error bars Commented Oct 7, 2014 at 21:27
  • That's certainly a workaround, I'll keep it in mind as a last resort. Commented Oct 8, 2014 at 9:10
  • Well worth getting in touch on the matplotlib forums, there may be such functionality already in development. If not, and you make a reasonable workaround, I'm sure they would be pleased to hear from you. Commented Oct 8, 2014 at 14:06

2 Answers 2

10

There is clearly example on forum http://mple.m-artwork.eu/home/posts/simple3dplotwith3derrorbars

Here is the code but is not built-in functionality:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d

fig = plt.figure(dpi=100)
ax = fig.add_subplot(111, projection='3d')



#data
fx = [0.673574075,0.727952994,0.6746285]
fy = [0.331657721,0.447817839,0.37733386]
fz = [18.13629648,8.620699842,9.807536512]

#error data
xerror = [0.041504064,0.02402152,0.059383144]
yerror = [0.015649804,0.12643117,0.068676131]
zerror = [3.677693713,1.345712547,0.724095592]

#plot points
ax.plot(fx, fy, fz, linestyle="None", marker="o")

#plot errorbars
for i in np.arange(0, len(fx)):
    ax.plot([fx[i]+xerror[i], fx[i]-xerror[i]], [fy[i], fy[i]], [fz[i], fz[i]], marker="_")
    ax.plot([fx[i], fx[i]], [fy[i]+yerror[i], fy[i]-yerror[i]], [fz[i], fz[i]], marker="_")
    ax.plot([fx[i], fx[i]], [fy[i], fy[i]], [fz[i]+zerror[i], fz[i]-zerror[i]], marker="_")

#configure axes
ax.set_xlim3d(0.55, 0.8)
ax.set_ylim3d(0.2, 0.5)
ax.set_zlim3d(8, 19)

plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

Yeap, drawing the errorbars this way still seems to be the only way to do this. It's a very neat example, I'll mark it as accepted.
2

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.