本文介绍了Matplotlib RuntimeWarning 显示 3D 绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本分析数据集,然后输出xyz数据.为了了解数据的分布,我想在3d图中对其进行可视化.由于我没有使用 matplotlib 的经验,我只是从

这是我的代码:

from mpl_toolkits.mplot3d 导入 Axes3D从 matplotlib 导入 cm导入matplotlib.pyplot作为plt从matplotlib.mlab导入griddata将numpy导入为np无花果= plt.figure()ax = fig.gca(projection='3d')数据 = np.genfromtxt('plot.txt')x = 数据[:,0]y =数据[:,1]z =数据[:,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='线性')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))color.colorbar(surf)plt.show()

我编辑了源代码,在违规行之前打印了xa,它输出:

[ nan nan nan nan nan nan nan nan nan nan nan 256.256. 256. 256. 256. 256. 256. 256. 南南 256. 256. 256.256. 256. 256. 256. 256. 南南 256. 256. 256. 256. 256.256. 256. 256. 南南 256. 256. 256. 256. 256. 256. 256.256.南南256.256.256.256.256.256.256.256.南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 值,但我不确定它们来自哪里.

解决方案

问题在于 griddata 无法生成网格边缘的数据.在内部通过屏蔽输出数组来避免这种情况.但是,对于掩码数组,比较 xa<确定颜色所需的0 是不可能的.

这里的解决方案是从绘制中排除边缘.

  ax.plot_surface(X [1:-1,1:-1],Y [1:-1,1:-1],Z [1:-1,1:-1])

完整示例:

from mpl_toolkits.mplot3d 导入 Axes3D从 matplotlib 导入 cm导入matplotlib.pyplot作为plt从matplotlib.mlab导入griddata将numpy导入为np无花果= plt.figure()ax = fig.gca(projection='3d')数据 = np.genfromtxt('plot.txt')x = 数据[:,0]y =数据[:,1]z =数据[:,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 ='线性')surf = ax.plot_surface(X[1:-1,1:-1], Y[1:-1,1:-1], Z[1:-1,1:-1],rstride = 5,cstride = 5,cmap = cm.jet,线宽=1,抗锯齿=真)ax.set_zlim3d(np.min(Z),np.max(Z))color.colorbar(surf)plt.show()

I have a script which analyses a dataset and then outputs xyz data. In order to understand the distribution of the data, I want to visualize it in a 3d plot. As I have no experience what so ever with using matplotlib, I just copied the code from here and expected it to work with my text file which looks like this:

-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
...

However, once I start the script, I get the following error which leads to the colorbar being displayed incorrectly:

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

Furthermore, the plot has these triangles on its edges. I'm not sure whether they are a consequence of the above mentioned error as well.This is the output:

Here's my code:

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()


EDIT 1:I edited the source code to print xa before the offending line, which outputs:

[  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]

So I clearly have some NaN values here, but I'm not sure where they come from.

解决方案

The problem is that griddata cannot produce data for the edges of the grid. This is circumvented internally by masking the output array. However, for a masked array, a comparison xa < 0, which is needed to determine the colors, is not possible.

The solution here would be to exclude the edges from plotting.

ax.plot_surface(X[1:-1,1:-1], Y[1:-1,1:-1], Z[1:-1,1:-1])

Complete example:

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[1:-1,1:-1], Y[1:-1,1:-1], Z[1:-1,1:-1],
                       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()

这篇关于Matplotlib RuntimeWarning 显示 3D 绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 16:31