I took the apodemus code , which apparently works, unpacked it and applied it to the original question to give it a direct answer. My code could certainly be cleared, in particular the loop in getDistances()
, but it solves the presented problem and should be much easier to follow. To guess, the distance to the viewer, that is, the distance to the camera, must be determined by calling sphview()
and sph2cart()
. Then, the distances of all bars from the camera should be calculated by calling getDistances()
. After that, the bars must be drawn once and once, and, to a decisive extent, each z-order must be explicitly set based on predetermined distances.
If the resulting graph is rotated in the graph window, it may not be updated correctly. However, pre-setting the location of the camera allows you to arbitrarily create arbitrary initial views without errors. (Perhaps there is a callback mechanism that can be called to force the z-orders of the bars to be explicitly recounted, but I don’t know such an API.) The camera position can be pre-set by going azim
and elev
to fig.add_subplot()
. You can change its distance by setting the dist
field of the Axes instance returned by fig.add_subplot()
.
The following is the graph created by the updated code applied to the original question:

import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import matplotlib.colors as colors import matplotlib.cm as cmx
This approach (in contrast to using Mayavi to process a 3D drawing, for example) allows you to save the appearance of matplotlib in the graphic itself, as well as its decorations, such as axis numbers, labels and legends.
source share