1
+ '''
2
+ Demonstrate using pathpatch_2d_to_3d to 'draw' shapes and text on a 3D plot.
3
+ '''
4
+
1
5
import matplotlib .pyplot as plt
2
6
from matplotlib .patches import Circle , PathPatch
3
7
# register Axes3D class with matplotlib by importing Axes3D
8
12
9
13
10
14
def text3d (ax , xyz , s , zdir = "z" , size = None , angle = 0 , usetex = False , ** kwargs ):
15
+ '''
16
+ Plots the string 's' on the axes 'ax', with position 'xyz', size 'size',
17
+ and rotation angle 'angle'. 'zdir' gives the axis which is to be treated
18
+ as the third dimension. usetex is a boolean indicating whether the string
19
+ should be interpreted as latex or not. Any additional keyword arguments
20
+ are passed on to transform_path.
11
21
22
+ Note: zdir affects the interpretation of xyz.
23
+ '''
12
24
x , y , z = xyz
13
25
if zdir == "y" :
14
26
xy1 , z1 = (x , z ), y
@@ -28,26 +40,28 @@ def text3d(ax, xyz, s, zdir="z", size=None, angle=0, usetex=False, **kwargs):
28
40
fig = plt .figure ()
29
41
ax = fig .add_subplot (111 , projection = '3d' )
30
42
43
+ # Draw a circle on the x=0 'wall'
31
44
p = Circle ((5 , 5 ), 3 )
32
45
ax .add_patch (p )
33
46
art3d .pathpatch_2d_to_3d (p , z = 0 , zdir = "x" )
34
47
35
-
48
+ # Manually label the axes
36
49
text3d (ax , (4 , - 2 , 0 ), "X-axis" , zdir = "z" , size = .5 , usetex = False ,
37
50
ec = "none" , fc = "k" )
38
51
text3d (ax , (12 , 4 , 0 ), "Y-axis" , zdir = "z" , size = .5 , usetex = False ,
39
52
angle = .5 * 3.14159 , ec = "none" , fc = "k" )
40
53
text3d (ax , (12 , 10 , 4 ), "Z-axis" , zdir = "y" , size = .5 , usetex = False ,
41
54
angle = .5 * 3.14159 , ec = "none" , fc = "k" )
42
55
56
+ # Write a Latex formula on the z=0 'floor'
43
57
text3d (ax , (1 , 5 , 0 ),
44
58
r"$\displaystyle G_{\mu\nu} + \Lambda g_{\mu\nu} = "
45
59
r"\frac{8\pi G}{c^4} T_{\mu\nu} $" ,
46
60
zdir = "z" , size = 1 , usetex = True ,
47
61
ec = "none" , fc = "k" )
48
62
49
- ax .set_xlim3d (0 , 10 )
50
- ax .set_ylim3d (0 , 10 )
51
- ax .set_zlim3d (0 , 10 )
63
+ ax .set_xlim (0 , 10 )
64
+ ax .set_ylim (0 , 10 )
65
+ ax .set_zlim (0 , 10 )
52
66
53
67
plt .show ()
0 commit comments