Wrong overlay in 3D plot

I have a really complex 3D plot overlaying a PolyCollection (representing land polygons), a contourf at the surface, which should appear on top of the former, and finally a bar3d.

I’m making different plots in a date range loop. For some dates the plot looks just fine, see e.g. this picture

Unfortunately for some of the dates the contourf appears on top of the bar3d object:

I understand the 3D engine may introduce artifacts at some viewing angles (mplot3d FAQ — Matplotlib 3.10.1 documentation) however, if that would be the case, I should see artifacts in every iteration of my loop and not only on certain dates…right?

Is there any workaround to correct ordering? Also trying out some experimental forks of matplotlib would be an option.

Unfortunately I’m using cartopy for the axis so I’m not sure I could just switch to a different package like Mayavi because it will be really hard to seamlessly port the code.

I could see the behavior changing if the data is changing, matplotlib tries to draw the different objects based on their distance to the camera (I don’t remember off the top of my head if it’s average distance of the data to the camera or the nearest). If your two objects are close to the same, then small differences could change which is drawn on top of the other.

To manually control the order that things are drawn, you can pass in the zorder arg to each of the plotting functions. There’s a 2D demo here, and 3D works similarly: Zorder Demo — Matplotlib 3.10.3 documentation

I tried to control the draw order using zorder but it doesn’t seem to be any effect on 3D scenes

Matplotlib rendering of a Poly3DCollection object is excellent and probably sufficient for most 3D visualizations. It is relatively easy when only one object is added to the axis3d using add_collection3d. So, just add one. If you have multiple objects, add them together to create a single object, for example. Addition can be made using something like the __add__ method listed on Github for S3Dlib. This technique also allows for complex animations in 3D using Matplotlib.