gboffi
gboffi

Reputation: 25023

Visibility of lines in a 3D plot

enter image description here

In my wishful thinking, the black line from (0, 0, 1) to (1, 0, 1) should be completely visible.

What should I do to get a sensible graph?


My mwe

In [37]: import numpy as np
    ...: import matplotlib.pyplot as plt
    ...: 
    ...: fig, ax = plt.subplots(subplot_kw=dict(projection="3d"), constrained_layout=1)
    ...: 
    ...: x = y = np.linspace(0, 1, 11)
    ...: X, Y = np.meshgrid(x, y)
    ...: Z = X+Y
    ...: 
    ...: ax.plot_surface(X, Y, Z)
    ...: ax.plot3D([0,1,1,0,0],[0,0,1,1,0],[1,1,1,1,1], color='k', lw=3)
    ...: plt.show()

Upvotes: 3

Views: 546

Answers (2)

Yulia V
Yulia V

Reputation: 3559

What I have noticed is that, the way matplotlib currently works, any polygon is either fully in front of any other polygon, or fully behind it.

If you know the intersection lines, you could display not your original polygons, but several polygons equivalent to the original ones broken by the intersection lines. Like this, the display will be correct.

It will probably be the same with plots etc., I have not checked, but polygons should be enough in the case you are asking about.enter image description here

import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d as a3

fig, ax = plt.subplots(subplot_kw=dict(projection="3d")) 
blue_coords_1 = [[0, 0, 0], [1, 0, 1/2], [0, 1, 1/2] ]
blue_coords_2 = [[1, 0, 1/2], [1, 1, 1], [0, 1, 1/2] ]

none_coords_1 = [[0, 0, 1/2], [1, 0, 1/2], [0, 1, 1/2]]
none_coords_2 = [[1, 0, 1/2], [1, 1, 1/2], [0, 1, 1/2]]

all_polys = a3.art3d.Poly3DCollection([none_coords_1, none_coords_2, blue_coords_1, blue_coords_2], facecolors=['blue', 'blue', 'red', 'red'], edgecolors=['none', 'none', 'black', 'black'])
ax.add_collection3d(all_polys)

plt.show()

Upvotes: 1

personaltest25
personaltest25

Reputation: 272

Do you mean like this? I've added "zorder" to your plot3D code.

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(subplot_kw=dict(projection="3d"), constrained_layout=1)
x = y = np.linspace(0, 1, 11)
X, Y = np.meshgrid(x, y)
Z = X+Y
 
ax.plot_surface(X, Y, Z)
ax.plot3D([0,1,1,0,0],[0,0,1,1,0],[1,1,1,1,1], color='k', lw=3, zorder=3)
plt.show()

Output:

enter image description here

Upvotes: 1

Related Questions