2

Edit: the problem I describe here no longer shows with current versions of matplotlib (2.1.1, edit made on 10 Sep 2019): most certainly a bug that has been fixed since then

I want to have a line plot (drawn with Axis.plot()) that is partially covered by the surface generated by Axis.plot_surface(). I wrote the following script:

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

resolution = 100
ax = plt.gca(projection="3d")
x, y = np.meshgrid( np.linspace(-1.0, 1.0, resolution) ,
                    np.linspace(-1.0, 1.0, resolution) )
phi = (1.0 - x)*(1.0 - y) / 4.0
ax.plot([-1.0,1.0], [-1.0,1.0], [0.0,0.0], color="red")
ax.plot_surface(x, y, phi, linewidth=0, antialiased=False)
plt.show()

and made sure to have the call to plot() before the one to plot_surface(). Nevertheless, it seems that the line plot always has the highest "zindex" and gets plotted over the surface. Here is what I obtain:

What I've got

Here is what I would like to have instead:enter image description here

How do I achieve this result? (Without using Gimp…)

Gael Lorieul
  • 3,006
  • 4
  • 25
  • 50
  • I can't find the question the last time this was asked, but IIRC there is no way to do without your own raytracer that figures out how much of that line should be visible from your particular viewpoint. – Paul Brodersen Jun 13 '18 at 09:46
  • Does that mean that it is either a missing feature or a bug? – Gael Lorieul Jun 13 '18 at 09:59
  • For the general case this is a missing feature. See [My 3D plot doesn’t look right at certain viewing angles](https://matplotlib.org/mpl_toolkits/mplot3d/faq.html#my-3d-plot-doesn-t-look-right-at-certain-viewing-angles). For this special case where you only have one line and one surface, there might still be some workaround of adjusting the drawing order or [masking the line depending on the viewing angle](https://stackoverflow.com/questions/41699494/how-to-obscure-a-line-behind-a-surface-plot-in-matplotlib). – ImportanceOfBeingErnest Jun 15 '18 at 00:13

1 Answers1

-1

I changed.

ax.plot_surface(x, y, phi, linewidth=0, antialiased=False)

with:

ax.plot_surface(x, y, phi, linewidth=0, antialiased=True)

and then I saw the whole red line It shows the plot with red line

Jason
  • 2,493
  • 2
  • 27
  • 27
Achim
  • 1
  • 2
  • The goal was to **not** see the red line because it goes below the surface. But matplotlib was showing the whole of it nevertheless. That being said, I ran the code in the original post again… and it worked! Most certainly a bug that was fixed between the current version of matplotlib, and the version I had back at the time of writting the original post. – Gael Lorieul Sep 10 '19 at 20:46