In my current project, I want to plot a 3D shape with pyplot. This is relatively straightforward:
The complication comes from the fact that I would like the figure to display in a straight 2D figure similar to this example:
That is to say, remove the 3D axes and ticks, the gridlines, and wrap everything in a flat 2D border. Is it possible to do this with Pyplot? You can find my code to generate the two figures below:
JavaScript
x
40
40
1
import matplotlib.pyplot as plt
2
import numpy as np
3
4
plt.figure()
5
6
x = np.asarray([0,1,1.5,0.5,0])
7
y = np.asarray([0,0,0.5,0.5,0])
8
9
# Plot 2D projection of cube
10
plt.plot(x,y,color='k')
11
plt.plot(x,y+1,color='k')
12
plt.plot([0,0],[0,1],color='k')
13
plt.plot([1,1],[0,1],color='k')
14
plt.plot([1.5,1.5],[0.5,1.5],color='k')
15
plt.plot([0.5,0.5],[0.5,1.5],color='k')
16
17
plt.title("2D projection of cube")
18
plt.axis('equal')
19
plt.tick_params(left=False,
20
bottom=False,
21
labelleft=False,
22
labelbottom=False)
23
24
# Now try the same thing in 3D
25
fig = plt.figure()
26
ax = fig.add_subplot(111, projection='3d')
27
28
x = np.asarray([0,1,1,0,0])
29
y = np.asarray([0,0,1,1,0])
30
31
# Plot 2D projection of cube
32
ax.plot3D(x,y,np.zeros(5),color='k')
33
ax.plot3D(x,y,np.ones(5),color='k')
34
ax.plot3D([0,0],[0,0],[0,1],color='k')
35
ax.plot3D([0,0],[1,1],[0,1],color='k')
36
ax.plot3D([1,1],[0,0],[0,1],color='k')
37
ax.plot3D([1,1],[1,1],[0,1],color='k')
38
39
plt.title("3D projection of cube")
40
Advertisement
Answer
add these lines :
JavaScript
1
17
17
1
color_tuple = (1, 1, 1, 0)
2
3
# make the panes transparent
4
ax.xaxis.set_pane_color(color_tuple)
5
ax.yaxis.set_pane_color(color_tuple)
6
ax.zaxis.set_pane_color(color_tuple)
7
8
# make the axis lines transparent
9
ax.w_xaxis.line.set_color(color_tuple)
10
ax.w_yaxis.line.set_color(color_tuple)
11
ax.w_zaxis.line.set_color(color_tuple)
12
13
# make the grid lines transparent
14
ax.set_xticks([])
15
ax.set_yticks([])
16
ax.set_zticks([])
17
output: