I have a plot consisting of a blue surface (plotted via plot_surface
) and a red sphere (plotted via scatter
). The zorder of the surface is set to 0, and the zorder of the sphere is set to 1 (though not setting any zorder values yields the same results).
You can see that the red sphere is to the left of the surface:
As I rotate the plot you can see the red sphere start to disappear behind the blue surface even though it's in front of it:
Until the red sphere completely disappears:
What is strange is that for certain angles/views the red sphere re-appears and is visible again, such as this one:
What's going on here? I've read some of the other plotting/zorder related issues but I haven't seen this type of behavior where one object is clearly behind/in-front of another and it isn't displayed correctly for most viewing angles.
If I make the blue surface transparent, you can see the red sphere behind the surface when it disappears in the plots below (so it seems like the plotting library actually thinks that it's behind the surface).
In order to plot 3D figures use matplotlib, we need to import the mplot3d toolkit, which adds the simple 3D plotting capabilities to matplotlib. Once we imported the mplot3d toolkit, we could create 3D axes and add data to the axes. Let's first create a 3D axes.
Matplotlib can also handle 3D plots by allowing the use of a Z axis. We've already created a 2D scatter plot above, but in this example we'll create a 3D scatter plot: Watch video here.
To generate an interactive 3D plot first import the necessary packages and create a random dataset. Now using Axes3D(figure) function from the mplot3d library we can generate a required plot directly. Pass the data to the 3D plot and configure the title and labels.
The drawing order of artists is determined by their zorder attribute, which is a floating point number. Artists with higher zorder are drawn on top. You can change the order for individual artists by setting their zorder . The default value depends on the type of the Artist: Artist.
This is still an issue in Matplotlib 1.5.3 (2016). The alternative that @tacaswell (who is a co-lead on Matplotlib dev) recommends is to handle 3D plotting using Mayavi which is relatively unique amongst Python plotting libraries in that it does not use Matplotlib as a backend like many other projects do (Pandas, Seaborn, ggplot).
I was able to install Mayavi on OSX with a minimum of fuss using Homebrew and pip.
#/bin/bash
# vtk is a mayavi requirement
brew install vtk
pip install mayavi
# Port your matplotlib code to mayavi
# Profit...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With