matplotlib contourf3d plot_surface vs. trisurf

时间:2016-06-13 15:05:43

标签: python python-3.x matplotlib mplot3d

我正在尝试使用具有以下结构的this进行data

 0.90000000   0.90000000 -2133.80472139
 0.90000000   0.95000000 -2133.84134433
 ...
 1.87500000   1.82500000 -2133.96171262
 1.87500000   1.87500000 -2133.95550450

使用以下代码,我已部分成功。但是,我无法在x-y,x-z和y-z平面上绘制轮廓。我不得不使用plot_trisurf,因为plot_surface选项对这些数据不起作用(我真的不知道为什么)。创建一个np.meshgrid没有帮助(在将列表转换为np.array之后)。

import numpy as np
import sys
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm

filename = sys.argv[1]

with open(filename+'.dat', 'r') as f:
    x = []
    y = []
    z = []
    for line in f:
        data = line.split()
        x.append((float(data[0])))
        y.append((float(data[1])))
        z.append((float(data[2])))

zz = [627.503*(i+2134.070983645239) for i in z]

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(x, y, zz, cmap=cm.jet, linewidth=0.1)
ax.dist=12
ax.view_init(30, 45)
ax.set_xlim(0.9, 1.9)
ax.set_ylim(0.9, 1.9)
ax.set_zlim(0, 170)
plt.show()

enter image description here

对于我如何在x-y位置上绘制轮廓以及在x-z和y-z上绘制投影,您有什么想法吗?

1 个答案:

答案 0 :(得分:3)

你必须使用三角形轮廓和填充三角形轮廓: tricontourtricontourfhttp://matplotlib.org/examples/pylab_examples/tricontour_demo.html

我已经添加了您使用tricontourf

绘制表面到XY的投影
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(x, y, zz, cmap=cm.jet, linewidth=0.1)
# projection of a surface to XY
ax.tricontourf(x, y, zz, zdir='z', offset=-1, cmap=cm.coolwarm)

ax.dist=12
ax.view_init(30, 45)
ax.set_xlim(0.9, 1.9)
ax.set_ylim(0.9, 1.9)
ax.set_zlim(0, 170)
plt.show()

enter image description here