5

I am using the solution proposed here to plot an image in 3D using matplotlib. However, even for very reasonable image sizes (128x128), the refresh rate is annoyingly slow. On my computer, the following cannot go higher than 2 frames/s.

import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import numpy as np
X, Y = np.meshgrid(np.arange(128), np.arange(128))
Z = np.zeros_like(X)
im = np.sin(X/10 + Y/100)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=plt.cm.BrBG(im), shade=False)
plt.show()

Is there any way to accelerate the above plot? I understand that mplot3d does not support hardware acceleration, but I feel the simple plot above ought to be faster even on CPU.

P-Gn
  • 23,115
  • 9
  • 87
  • 104
  • What makes you "feel" that? Any source for that? Matplotlib 3D plots are projected into 2D when being drawn. This would be repeatedly done when updating the plot, resulting in slow updating speed. If performance is an issue, don't use mplot3d. – ImportanceOfBeingErnest Jul 14 '17 at 09:01

1 Answers1

4

You can try mayaVi library for better interactive data visualization.

#import matplotlib.pyplot as plt
#from mpl_toolkits import mplot3d
import numpy as np
from mayavi import mlab

X, Y = np.meshgrid(np.arange(128), np.arange(128))
Z = np.zeros_like(X)
im = np.sin(X/10 + Y/100)

#fig = plt.figure()
#x = fig.add_subplot(111, projection='3d')

src = mlab.pipeline.array2d_source(im)
warp = mlab.pipeline.warp_scalar(src)
normals = mlab.pipeline.poly_data_normals(warp)
surf = mlab.pipeline.surface(normals)
mlab.show()


#ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=plt.cm.BrBG(im), shade=False)

#plt.show()

MayaVi Documentation

Vibhutha Kumarage
  • 1,372
  • 13
  • 26