在matplotlib中的3D绘图上画一条线

时间:2013-12-26 08:50:17

标签: 3d matplotlib

我有一个看起来非常类似于this one的3D图,但我想在底部等高线图上添加两条线。我希望两条线交叉并指向特定的(x,y)值。

我可以在那里画一条线,但它总是在等高线图下面,尽管我做了很多尝试。任何人都可以帮我画 等高线图?我在下面粘贴我的代码。该线仅在从轮廓伸出的部分可见。

#!/usr/bin/env python

import sys, re, math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import LineCollection
from matplotlib import cm
from matplotlib.mlab import griddata
import numpy as np

x = []
y = []
z = []
for i in range(0,628,10) :
  for j in range(0,628,10) :
    x.append(i*0.01)
    y.append(j*0.01)
    z.append(math.sin(x[-1]*y[-1])*20)

fig = plt.figure()
ax = fig.gca(projection='3d')

xi = np.linspace(min(x), max(x))
yi = np.linspace(min(y), max(y))

X, Y = np.meshgrid(xi, yi)
Z = griddata(x, y, z, xi, yi)
ax.autoscale(False)
ax.view_init(elev=30, azim=-140)

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.cool,
               linewidth=0.1, antialiased=True, shade=True,alpha=0.8)
cset = ax.contourf(X, Y, Z, zdir='z', offset=-100, cmap=cm.cool, cstride=1,
        linewidth=0.1, antialiased=True, shade=True)

ax.set_xlim3d(0, math.pi*2)
ax.set_ylim3d(0, math.pi*2)
ax.set_zlim3d(-100, 20)

xi = [0,6]
yi = [3,3]
l=zip(xi,yi)
lines = LineCollection((l,l),zorder=10000,color='k')
ax.add_collection3d(lines,zs=-90)

plt.show()

2 个答案:

答案 0 :(得分:1)

因为Axe3D将通过3d投影计算每个3d对象的zorder属性。要禁用此功能,您可以创建FixZorderCollection类并更改3d行的__class__属性。这是一个完整的例子:

import sys, re, math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import LineCollection
from matplotlib import cm
from matplotlib.mlab import griddata
import numpy as np

x = []
y = []
z = []
for i in range(0,628,10) :
  for j in range(0,628,10) :
    x.append(i*0.01)
    y.append(j*0.01)
    z.append(math.sin(x[-1]*y[-1])*20)

fig = plt.figure()
ax = fig.gca(projection='3d')

xi = np.linspace(min(x), max(x))
yi = np.linspace(min(y), max(y))

X, Y = np.meshgrid(xi, yi)
Z = griddata(x, y, z, xi, yi)
ax.autoscale(False)
ax.view_init(elev=30, azim=-140)

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.cool,
               linewidth=0.1, antialiased=True, shade=True,alpha=0.8)
cset = ax.contourf(X, Y, Z, zdir='z', offset=-100, cmap=cm.cool, cstride=1,
        linewidth=0.1, antialiased=True, shade=True)

ax.set_xlim3d(0, math.pi*2)
ax.set_ylim3d(0, math.pi*2)
ax.set_zlim3d(-100, 20)

xi = [0,6]
yi = [3,3]
l=zip(xi,yi)
lines = LineCollection((l,l),zorder=1000,color='k',lw=3)
ax.add_collection3d(lines,zs=-90)

from mpl_toolkits.mplot3d.art3d import Line3DCollection

class FixZorderCollection(Line3DCollection):
    _zorder = 1000

    @property
    def zorder(self):
        return self._zorder

    @zorder.setter
    def zorder(self, value):
        pass

ax.collections[-1].__class__ = FixZorderCollection

plt.show()

输出:

enter image description here

答案 1 :(得分:1)

HYRY的回答帮助了我。由于tnorgd想知道如何为散点图(我还不能发表评论),我在这里给出答案。 pyplot.scatter在投影“3d”中创建Patch3DCollection。所以相同的技巧将起作用:

from mpl_toolkits.mplot3d.art3d import Patch3DCollection
class FixZorderScatter(Patch3DCollection):
    _zorder = 2000
    @property
    def zorder(self):
        return self._zorder
    @zorder.setter
    def zorder(self, value):
        pass