【发布时间】:2017-10-20 07:56:25
【问题描述】:
我有一个分析数据集然后输出 xyz 数据的脚本。为了了解数据的分布,我想在 3d 图中将其可视化。由于我没有使用 matplotlib 的经验,所以我只是从 here 复制了代码,并希望它可以与 my text file 一起使用,如下所示:
-0.9 -0.9 483
-0.9 -0.7 224
-0.9 -0.5 156
-0.9 -0.3 153
-0.9 -0.1 174
-0.9 0.1 268
-0.9 0.3 95
-0.9 0.5 59
-0.9 0.7 50
-0.9 0.9 199
-0.7 -0.9 917
-0.7 -0.7 244
-0.7 -0.5 208
-0.7 -0.3 148
-0.7 -0.1 139
-0.7 0.1 98
-0.7 0.3 52
-0.7 0.5 56
-0.7 0.7 60
-0.7 0.9 221
...
但是,一旦我启动脚本,我会收到以下错误,导致颜色条显示不正确:
Warning (from warnings module):
File "C:\Program Files\Python35\lib\site-packages\matplotlib\colors.py", line 496
cbook._putmask(xa, xa < 0.0, -1)
RuntimeWarning: invalid value encountered in less
此外,绘图的边缘有这些三角形。我不确定它们是否也是上述错误的结果。 这是输出:
这是我的代码:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
data = np.genfromtxt('plot.txt')
x = data[:,0]
y = data[:,1]
z = data[:,2]
xi = np.linspace(-1, 1)
yi = np.linspace(-1, 1)
X, Y = np.meshgrid(xi, yi)
Z = griddata(x, y, z, xi, yi, interp='linear')
surf = ax.plot_surface(X, Y, Z, rstride=5, cstride=5, cmap=cm.jet,
linewidth=1, antialiased=True)
ax.set_zlim3d(np.min(Z), np.max(Z))
fig.colorbar(surf)
plt.show()
编辑 1: 我编辑了源代码以在有问题的行之前打印 xa ,输出:
[ nan nan nan nan nan nan nan nan nan nan nan 256.
256. 256. 256. 256. 256. 256. 256. nan nan 256. 256. 256.
256. 256. 256. 256. 256. nan nan 256. 256. 256. 256. 256.
256. 256. 256. nan nan 256. 256. 256. 256. 256. 256. 256.
256. nan nan 256. 256. 256. 256. 256. 256. 256. 256. nan
nan 256. 256. 256. 256. 256. 256. 256. 256. nan nan 256.
256. 256. 256. 256. 256. 256. 256. nan nan 256. 256. 256.
256. 256. 256. 256. 256. nan nan nan nan nan nan nan
nan nan nan nan]
所以我这里显然有一些 NaN 值,但我不确定它们来自哪里。
【问题讨论】:
-
这是一个警告,而不是错误,因此你确实得到了一个情节。如果没有minimal reproducible example 的问题,真的很难知道发生了什么,即我们没有您的数据。最好尝试在代码中生成一些数据来重现问题。
-
如果您查看源代码并将
print(xa)放在有问题的行之前,您会看到数组包含 nan 值,这是因为您的Z包含 nan 值。 -
@ImportanceOfBeingErnest 感谢您的快速回复和编辑我的帖子。我确实向您提供了我的数据。 link above 包含我拥有的所有 xyz 坐标。
-
@Reti43 谢谢你的提示。我相应地编辑了我的问题!
标签: python python-3.x matplotlib