Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b800d3

Browse files
committed
add PathPatch3d in mplot3d.art3d
svn path=/trunk/matplotlib/; revision=7842
1 parent eab892e commit 0b800d3

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import matplotlib.pyplot as plt
2+
from matplotlib.patches import Circle, PathPatch
3+
from mpl_toolkits.mplot3d import Axes3D
4+
import mpl_toolkits.mplot3d.art3d as art3d
5+
from matplotlib.text import TextPath
6+
from matplotlib.transforms import Affine2D
7+
8+
9+
def text3d(ax, (x, y, z), s, zdir="z", size=None, angle=0, usetex=False,
10+
**kwargs):
11+
12+
if zdir == "y":
13+
xy1, z1 = (x, z), y
14+
elif zdir == "y":
15+
xy1, z1 = (y, z), x
16+
else:
17+
xy1, z1 = (x, y), z
18+
19+
text_path = TextPath((0, 0), s, size=size, usetex=usetex)
20+
trans = Affine2D().rotate(angle).translate(xy1[0], xy1[1])
21+
22+
p1 = PathPatch(trans.transform_path(text_path), **kwargs)
23+
ax.add_patch(p1)
24+
art3d.pathpatch_2d_to_3d(p1, z=z1, zdir=zdir)
25+
26+
27+
fig = plt.figure()
28+
ax = Axes3D(fig)
29+
30+
p = Circle((5, 5), 3)
31+
ax.add_patch(p)
32+
art3d.pathpatch_2d_to_3d(p, z=0, zdir="x")
33+
34+
35+
text3d(ax, (4, -2, 0), "X-axis", zdir="z", size=.5, usetex=False,
36+
ec="none", fc="k")
37+
text3d(ax, (12, 4, 0), "Y-axis", zdir="z", size=.5, usetex=False, angle=.5*3.14159,
38+
ec="none", fc="k")
39+
text3d(ax, (12, 10, 4), "Z-axis", zdir="y", size=.5, usetex=False, angle=.5*3.14159,
40+
ec="none", fc="k")
41+
42+
text3d(ax, (1, 5, 0),
43+
r"$\displaystyle G_{\mu\nu} + \Lambda g_{\mu\nu} = \frac{8\pi G}{c^4} T_{\mu\nu} $",
44+
zdir="z", size=1, usetex=True,
45+
ec="none", fc="k")
46+
47+
ax.set_xlim3d(0, 10)
48+
ax.set_ylim3d(0, 10)
49+
ax.set_zlim3d(0, 10)
50+
51+
plt.show()
52+

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,31 @@ def do_3d_projection(self, renderer):
217217
def draw(self, renderer):
218218
Patch.draw(self, renderer)
219219

220+
221+
class PathPatch3D(Patch3D):
222+
'''
223+
3D PathPatch object.
224+
'''
225+
226+
def __init__(self, path, **kwargs):
227+
zs = kwargs.pop('zs', [])
228+
zdir = kwargs.pop('zdir', 'z')
229+
Patch.__init__(self, **kwargs)
230+
self.set_3d_properties(path, zs, zdir)
231+
232+
def set_3d_properties(self, path, zs=0, zdir='z'):
233+
Patch3D.set_3d_properties(self, path.vertices, zs=zs, zdir=zdir)
234+
self._code3d = path.codes
235+
236+
def do_3d_projection(self, renderer):
237+
s = self._segment3d
238+
xs, ys, zs = zip(*s)
239+
vxs, vys,vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
240+
self._path2d = mpath.Path(zip(vxs, vys), self._code3d)
241+
# FIXME: coloring
242+
self._facecolor2d = self._facecolor3d
243+
return min(vzs)
244+
220245
def get_patch_verts(patch):
221246
"""Return a list of vertices for the path of a patch."""
222247
trans = patch.get_patch_transform()
@@ -233,6 +258,15 @@ def patch_2d_to_3d(patch, z=0, zdir='z'):
233258
patch.__class__ = Patch3D
234259
patch.set_3d_properties(verts, z, zdir)
235260

261+
def pathpatch_2d_to_3d(pathpatch, z=0, zdir='z'):
262+
"""Convert a PathPatch to a PathPatch3D object."""
263+
path = pathpatch.get_path()
264+
trans = pathpatch.get_patch_transform()
265+
266+
mpath = trans.transform_path(path)
267+
pathpatch.__class__ = PathPatch3D
268+
pathpatch.set_3d_properties(mpath, z, zdir)
269+
236270
class Patch3DCollection(PatchCollection):
237271
'''
238272
A collection of 3D patches.

0 commit comments

Comments
 (0)