Python matplotlib颜色在不使用等高线的情况下从x、y、z数据集进行三维打印

Python matplotlib颜色在不使用等高线的情况下从x、y、z数据集进行三维打印,python,matplotlib,Python,Matplotlib,就我的一生而言,我不知道怎样才能得到和我一样的结果 该链接不使用等高线生成彩色3d打印。如果我使用相同的技术,但是使用我自己的x,y,z数据集,我只得到一种颜色 差异一定是在我为绘图生成z数据的方式上 无论如何,使用以下方法: from mpl_toolkits.mplot3d import Axes3D from matplotlib.mlab import griddata from matplotlib import cm from matplotlib.ticker import Lin

就我的一生而言,我不知道怎样才能得到和我一样的结果

该链接不使用等高线生成彩色3d打印。如果我使用相同的技术,但是使用我自己的x,y,z数据集,我只得到一种颜色

差异一定是在我为绘图生成z数据的方式上

无论如何,使用以下方法:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib.mlab import griddata
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
import sys

def xyz_ret(file):
    f = open(file, 'r')

    xyz = []
    for i in f:
        ret = i.replace('\n','')
        xyz.append(map(float,(ret.split('\t'))))

    xyz =  np.array(xyz)   
    return xyz[:,0],xyz[:,1],xyz[:,2]     


x,y,z = xyz_ret('300.txt')

fig = plt.figure()
ax = fig.add_subplot(111, 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)

surf = ax.plot_surface(X, Y, Z, rstride=6, cstride=6, cmap=cm.jet,
        linewidth=0)

ax.set_zlim3d(min(z), max(z))

ax.w_zaxis.set_major_locator(LinearLocator(10))
ax.w_zaxis.set_major_formatter(FormatStrFormatter('%.03f'))

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()
数据集:

-2187.99902 9380.009151 0.0209
-2187.00111 2474.994061 0.022
-10755.98931 6119.598968 0.0296
-5781.347693 609.427388 0.0301
-8761.562524 1942.391853 0.0285
-5695.576244 1894.624701 0.0251
-3801.215106 1096.153308 0.0257
-1616.821487 2452.940102 0.0182
-5790.547537 2975.622971 0.022
-8095.18467 4074.330871 0.0208
-9997.367785 2771.330212 0.0264
-10547.5635 4397.127096 0.0251
-5781.706776 3984.545588 0.0191
-3346.855289 4347.670408 0.0172
-918.639762 4518.515925 0.0142
-892.428381 5850.710005 0.0143
-5844.499993 6516.904257 0.0204
-10877.96951 6015.755723 0.0265
-10813.37291 7704.306099 0.0302
-7991.878303 7733.626264 0.0223
-5861.073574 8725.943697 0.0217
-3188.107715 6997.19893 0.0206
-897.427629 7474.426336 0.0188
-1388.841321 8786.642046 0.0194
-3370.72325 8825.154803 0.0225
-8561.226722 8851.111988 0.0285
-10275.58972 8849.798032 0.0341
-5853.645621 10113.77051 0.0255
-8101.002878 10754.8429 0.0332
-5765.080546 11378.95524 0.0299
-3081.969839 10549.46676 0.0242
只显示一种颜色。还请注意,颜色栏没有刻度


你能解释一下我的问题是什么吗?

读取文本数据最简单的方法是通过genfromtxt:

data = np.genfromtxt('300.txt')
x = data[:,0]
y = data[:,1]
z = data[:,2]

sys
不是必需的。

我认为填充“不连续”曲面(griddata)有问题

代码:

请注意,如果考虑矩形区域以上的代码<代码>(席Xi Yi)< /代码>,则该代码工作正常。换句话说,如果你“切断”不规则的边缘

xi = np.linspace(-4000, -9000)
yi = np.linspace(4000, 9000)

我刚刚遇到了类似的问题

最后,我不得不使用(这是引用的,但链接不起作用)而不是griddata

对我来说,切割绘图区域的技巧不起作用,它总是用一种颜色

安装PyNGL时,请检查您是否拥有最新版本的numpy


祝您好运

我想说您没有使用Axes3D.plot\u曲面。导入Axes3D但不使用它。它在ax=fig.add_子批次(111,projection='3d')中使用,如果我删除Axes3D,脚本将在该行出错。我使用sys在编写测试代码时随机退出脚本。sys.exit()感谢您的反馈。我还在想办法。我想知道是否有其他方法可以不使用griddata获取z数据?上述情况的症状是否也会是颜色栏中缺少记号?我认为这两个问题是直接相关的。在示例代码中,显示的记号表示z高度的不同颜色。如果在示例代码中打印Z,则会显示一个列表数组,其中填充了数字;如果在上面的代码中打印Z,则会显示一个---列表。有什么想法吗?嗯,谢谢你找到了。现在如何修复破损的表面?我在此处发现其他人有相同的问题:。不幸的是,他的问题没有得到回答。表面没有破损,但没有为所有点指定
(xi,yi)
Z
值。我不知道如何填充不规则的表面。
mlab.griddata
已经被弃用,取而代之的是
scipy.interpolate.griddata
,它需要稍微不同的参数:
Z=griddata((x,y),Z,(x.flatte(),y.flatte(),'nearest')。重塑(50,50)
。(50是
np.linspace
的默认
num
xi = np.linspace(-4000, -9000)
yi = np.linspace(4000, 9000)