1

I am trying to plot a sphere in front of a FancyBboxPatch in matplotlib. However, the sphere is covered up by the FancyBBoxPatch. My minimal working example is as follows:

 #!/usr/bin/env python3                                                                                                                                                                                      

 import numpy as np
 import matplotlib.pyplot as plt
 from matplotlib.patches import FancyBboxPatch
 from mpl_toolkits.mplot3d import Axes3D
 import mpl_toolkits.mplot3d.art3d as art3d

 if __name__ == '__main__':

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

     for i in range(7):
         for j in range(7):

             box = FancyBboxPatch((i, j),
                                  1, 1,
                                  boxstyle = 'square,pad=-0.1',
                                  edgecolor = (0.333, 0.333, 0.333),
                                  facecolor = (0.333, 0.333, 0.333))
                                  #zorder = 0)                                                                                                                                                               

             axes.add_patch(box)
             art3d.pathpatch_2d_to_3d(box, z = 0, zdir = 'x')

     u = np.linspace(0, 2 * np.pi, 100)
     v = np.linspace(0, np.pi, 100)
     x = 0.3 * np.outer(np.cos(u), np.sin(v))
     y = 0.3 * np.outer(np.sin(u), np.sin(v))
     z = 0.3 * np.outer(np.ones(np.size(u)), np.cos(v))

     surface = axes.plot_surface(x + 1, y + 1, z + 5, color = 'red')
                                 #zorder = 100,                                                                                                                                                              
                                 #alpha = 1)                                                                                                                                                                 

     axes.set_xlim3d(0, 14)
     axes.set_ylim3d(0, 14)
     axes.set_zlim3d(0, 14)

     plt.show()

Which produces the plot:

enter image description here

In the plot, the sphere is completely covered by the FancyBboxPatches.

As you can see in portions of the script that are commented out, I have played around with the keyword arguments "alpha" and "zorder" (I don't want to change alpha for the FancyBboxPatches) to try to make the sphere be drawn on top of the FancyBboxPatches, but am unsuccessful.

How can I make the sphere be drawn in front of the FancyBboxPatches? Or will the answer be similar to Matplotlib 3D plot zorder issue in that I should try MayaVi?

Erdrick
  • 87
  • 1
  • 4
  • While there might be some way to solve this specific case for one specific viewing angle, the general tendency is the same as in the linked question and also like in the [mplot3d FAQ](https://matplotlib.org/mpl_toolkits/mplot3d/faq.html): matplotlib has never been designed to be good at 3D rendering and mayavi might be the better option. – ImportanceOfBeingErnest Jul 29 '17 at 20:24

0 Answers0