Search code examples
pythonmatplotlibvoxels

'Axes3DSubplot' object has no attribute 'voxels'


I'm trying to use matplotlib to display some 3d perlin noise. I have read that the voxels method from Axes3DSubplot could be used to simply display values. However, when I try and call ax.voxels(voxels, facecolors=colors, edgecolor='k'), it throws the exception AttributeError: 'Axes3DSubplot' object has no attribute 'voxels'. Here is my code:

import noise
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x, y, z = np.indices((8,8,8))
voxels = np.zeros((8,8,8), dtype=np.bool)

for xp in range(8):
    for yp in range(8):
        for zp in range(8):
            voxels[xp,yp,zp] = True if abs(noise.pnoise3(xp/8,yp/8,zp/8)) > 0.5 else False

colors = np.empty(voxels.shape, dtype=object)
colors[voxels] = 'green'

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.voxels(voxels, facecolors=colors, edgecolor='k')  #EXCEPTION


plt.show()

My python version is 3.6.2 (Anaconda 64-bit). My matplotlib version is 2.0.2. I have used both the ipynb (module://backend_interagg) and Qt5Agg backends, which both give the same problem. I'm running Windows 10.


Solution

  • The voxels method has been introduced in matplotlib 2.1.

    Any earlier version of matplotlib does not have this method available.