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

Skip to content

Commit 787755e

Browse files
committed
mplot3d: support contours in directions other than z
svn path=/trunk/matplotlib/; revision=8019
1 parent d5eb691 commit 787755e

4 files changed

Lines changed: 43 additions & 10 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from mpl_toolkits.mplot3d import axes3d
2+
import matplotlib.pyplot as plt
3+
4+
fig = plt.figure()
5+
ax = axes3d.Axes3D(fig)
6+
X, Y, Z = axes3d.get_test_data(0.05)
7+
ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
8+
cset = ax.contour(X, Y, Z, zdir='z', offset=-100)
9+
cset = ax.contour(X, Y, Z, zdir='x', offset=-40)
10+
cset = ax.contour(X, Y, Z, zdir='y', offset=40)
11+
12+
ax.set_xlabel('X')
13+
ax.set_xlim3d(-40, 40)
14+
ax.set_ylabel('Y')
15+
ax.set_ylim3d(-40, 40)
16+
ax.set_zlabel('Z')
17+
ax.set_zlim3d(-100, 100)
18+
19+
plt.show()
20+

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,15 @@ def juggle_axes(xs, ys, zs, zdir):
437437
Reorder coordinates so that zdir
438438
"""
439439
if zdir == 'x':
440+
return ys, zs, xs
441+
elif zdir == '-x':
440442
return zs, xs, ys
443+
441444
elif zdir == 'y':
442-
return xs, zs, ys
445+
return zs, xs, ys
446+
elif zdir == '-y':
447+
return ys, zs, xs
448+
443449
else:
444450
return xs, ys, zs
445451

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -839,10 +839,11 @@ def contour(self, X, Y, Z, levels=10, **kwargs):
839839
========== ================================================
840840
*X*, *Y*, Data values as numpy.arrays
841841
*Z*
842-
*levels* Number of levels to use, defaults to 10. Can
843-
also be a tuple of specific levels.
844842
*extend3d* Whether to extend contour in 3D (default: False)
845843
*stride* Stride (step size) for extending contour
844+
*zdir* The direction to use: x, y or z (default)
845+
*offset* If specified plot a projection of the contour
846+
lines on this position in plane normal to zdir
846847
========== ================================================
847848
848849
Other keyword arguments are passed on to
@@ -851,16 +852,22 @@ def contour(self, X, Y, Z, levels=10, **kwargs):
851852

852853
extend3d = kwargs.pop('extend3d', False)
853854
stride = kwargs.pop('stride', 5)
854-
nlevels = kwargs.pop('nlevels', 15)
855+
zdir = kwargs.pop('zdir', 'z')
856+
offset = kwargs.pop('offset', None)
855857

856858
had_data = self.has_data()
857-
cset = Axes.contour(self, X, Y, Z, levels, **kwargs)
858859

860+
jX, jY, jZ = art3d.juggle_axes(X, Y, Z, zdir)
861+
cset = Axes.contour(self, jX, jY, jZ, **kwargs)
862+
863+
zdir = '-' + zdir
859864
if extend3d:
860865
self._3d_extend_contour(cset, stride)
861866
else:
862867
for z, linec in zip(cset.levels, cset.collections):
863-
art3d.line_collection_2d_to_3d(linec, z)
868+
if offset is not None:
869+
z = offset
870+
art3d.line_collection_2d_to_3d(linec, z, zdir=zdir)
864871

865872
self.auto_scale_xyz(X, Y, Z, had_data)
866873
return cset

lib/mpl_toolkits/mplot3d/axis3d.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ class Axis(maxis.XAxis):
5757

5858
# Some properties for the axes
5959
_AXINFO = {
60-
'x': {'i': 0, 'tickdir': 1,
60+
'x': {'i': 0, 'tickdir': 1, 'juggled': (1, 0, 2),
6161
'color': (0.95, 0.95, 0.95, 0.5)},
62-
'y': {'i': 1, 'tickdir': 0,
62+
'y': {'i': 1, 'tickdir': 0, 'juggled': (0, 1, 2),
6363
'color': (0.90, 0.90, 0.90, 0.5)},
64-
'z': {'i': 2, 'tickdir': 0,
64+
'z': {'i': 2, 'tickdir': 0, 'juggled': (0, 2, 1),
6565
'color': (0.925, 0.925, 0.925, 0.5)},
6666
}
6767

@@ -191,7 +191,7 @@ def draw(self, renderer):
191191
minmax = np.where(highs, maxs, mins)
192192

193193
# Draw main axis line
194-
juggled = art3d.juggle_axes(0, 2, 1, self.adir)
194+
juggled = info['juggled']
195195
edgep1 = minmax.copy()
196196
edgep1[juggled[0]] = get_flip_min_max(edgep1, juggled[0], mins, maxs)
197197

0 commit comments

Comments
 (0)