Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib 3D plot zorder issue

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: enter image description here

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:

enter image description here

Until the red sphere completely disappears: enter image description here

What is strange is that for certain angles/views the red sphere re-appears and is visible again, such as this one: enter image description here

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).

like image 809
mattnedrich Avatar asked Apr 20 '14 23:04

mattnedrich


People also ask

Can we plot 3D plots matplotlib?

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.

Can matplotlib Pyplot Cannot be used to display 3D plots?

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.

How do I create an interactive 3D plot in matplotlib?

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.

What is matplotlib Zorder?

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.


1 Answers

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...
like image 177
RegularlyScheduledProgramming Avatar answered Oct 05 '22 20:10

RegularlyScheduledProgramming