带有数据的 Python 3D 绘图,发生错误 [英] Python 3D plot with data, error occured

查看:25
本文介绍了带有数据的 Python 3D 绘图,发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 ax.plot_surface 用手动输入数据(x,y,z)绘制3D图.即使我使用了网上找到的类似代码,我仍然遇到了一些错误.

I was trying to plot a 3D diagram with manual input data (x,y,z) using ax.plot_surface. Even though I used a similar code I found online, I still got some errors.

"Warning (from warnings module):
  File "D:\Program Files (x86)\Python\Python36\lib\site-packages\numpy\core\_methods.py", line 29
    return umr_minimum(a, axis, None, out, keepdims)
RuntimeWarning: invalid value encountered in reduce

Warning (from warnings module):
  File "D:\Program Files (x86)\Python\Python36\lib\site-packages\numpy\core\_methods.py", line 26
    return umr_maximum(a, axis, None, out, keepdims)
RuntimeWarning: invalid value encountered in reduce

Warning (from warnings module):
  File "D:\Program Files (x86)\Python\Python36\lib\site-packages\matplotlib\colors.py", line 489
    np.copyto(xa, -1, where=xa < 0.0)
RuntimeWarning: invalid value encountered in less"

即使有这个错误,也可以绘制图表.但这全是黑色的.而且,颜色条与z值不匹配.

Even with this errors, the diagram could be plotted. But it's all black. And somehow, the colorbar does not match the z values.

有人可以帮助我解决这个问题吗?感谢您的帮助.

Can anyone help me with this problem? I appreciate your help.

这是我使用的代码(具体代码如下所示):

This is the code I used (the exact code is shown below):

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = [1043.797,621.694,203.275,-213.783,-627.143,-1045.474,-1045.474,-628.403,-213.783,0.42,203.278,621.697,1043.801,1042.545,621.701,203.282,0.426,-213.778,-628.397,-1045.467,-0.834,1043.804,621.701,203.292,0.434,-213.77,-628.393,-1045.462,-1045.464,-628.395,-213.772,-0.829,203.29,621.707,1043.812,1043.807,621.706,203.287,-213.775,-628.398,-1045.466]

y = [-1210.936,-1211.146,-1210.931,-1210.819,-1210.916,-1210.916,-727.082,-726.768,-726.776,-726.883,-726.887,-727.101,-726.68,-242.741,-243.059,-242.846,-242.841,-242.732,-242.723,-243.037,19.801,241.133,241.025,241.248,241.148,241.154,241.167,241.07,725.216,725.208,724.565,725.401,724.976,724.97,724.975,1209.226,1209.324,1209.328,1209.338,1209.559,1209.254]

z = [3753.086,4054.802,4101.778,4064.706,3844.414,3614.887,4156.525,4184.521,4284.536,4269.797,4273.816,4298.024,4264.16,4224.935,4188.664,4200.863,4210.243,4164.851,4143.223,4148.073,3980.13,4094.025,4203.862,4260.099,4238.935,4233.248,4186.161,4072.293,4021.05,4311.022,4351.636,4359.61,4385.24,4382.892,4169.055,3927.979,4226.974,4237.096,4180.779,4082.677,3739.785]

x=np.asarray(x)
y=np.asarray(y)
N = 100
xi = np.linspace(x.min(), x.max(), N)
yi = np.linspace(y.min(), y.max(), N)
zi = scipy.interpolate.griddata((x, y), z, (xi[None,:], yi[:,None]), 
method='cubic')
xi, yi = np.meshgrid(xi,yi)
surf = ax.plot_surface(xi, yi, zi, cmap=plt.cm.hot)
plt.show()

推荐答案

由于网格上的插值,结果数组的最外点是 nan(即第一列和最后一列 & first和最后一行).虽然 nan 值可以在绘图时忽略,但不幸的是它们不能用于生成着色.为了能够使用颜色图,应该提供一个没有 nan 值的数组(严格来说这仅适用于 3D 绘图).

Due to the interpolation on the grid, the outmost points of the resulting array are nan (i.e. first and last column & first and last row). While nan values can be ignored for plotting, they are unfortunately not for producing the colorization. In order to be able to use a colormap, an array without nan values should be provided (this is strictly only true for 3D plots).

虽然通常有多个选项,例如替换值和遮罩,但这里最简单的是省去绘制的行和列.IE.代替 ax.plot_surface(xi,yi,zi,cmap ="hot"),您可以使用

While there are in general several options like replacing values and masking, here the easiest is to leave out the rows and columns from plotting. I.e. instead of ax.plot_surface(xi, yi, zi, cmap="hot") you can use

ax.plot_surface(xi[1:-1,1:-1], yi[1:-1,1:-1], zi[1:-1,1:-1], cmap="hot")

完整示例:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import scipy.interpolate


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = [1043.797,621.694,203.275,-213.783,-627.143,-1045.474,-1045.474,-628.403,-213.783,0.42,203.278,621.697,1043.801,1042.545,621.701,203.282,0.426,-213.778,-628.397,-1045.467,-0.834,1043.804,621.701,203.292,0.434,-213.77,-628.393,-1045.462,-1045.464,-628.395,-213.772,-0.829,203.29,621.707,1043.812,1043.807,621.706,203.287,-213.775,-628.398,-1045.466]

y = [-1210.936,-1211.146,-1210.931,-1210.819,-1210.916,-1210.916,-727.082,-726.768,-726.776,-726.883,-726.887,-727.101,-726.68,-242.741,-243.059,-242.846,-242.841,-242.732,-242.723,-243.037,19.801,241.133,241.025,241.248,241.148,241.154,241.167,241.07,725.216,725.208,724.565,725.401,724.976,724.97,724.975,1209.226,1209.324,1209.328,1209.338,1209.559,1209.254]

z = [3753.086,4054.802,4101.778,4064.706,3844.414,3614.887,4156.525,4184.521,4284.536,4269.797,4273.816,4298.024,4264.16,4224.935,4188.664,4200.863,4210.243,4164.851,4143.223,4148.073,3980.13,4094.025,4203.862,4260.099,4238.935,4233.248,4186.161,4072.293,4021.05,4311.022,4351.636,4359.61,4385.24,4382.892,4169.055,3927.979,4226.974,4237.096,4180.779,4082.677,3739.785]

x=np.asarray(x)
y=np.asarray(y)
N = 100
xi = np.linspace(x.min(), x.max(), N)
yi = np.linspace(y.min(), y.max(), N)
zi = scipy.interpolate.griddata((x, y), z, (xi[None,:], yi[:,None]), 
                                        method='cubic')

xi, yi = np.meshgrid(xi,yi)

surf = ax.plot_surface(xi[1:-1,1:-1], yi[1:-1,1:-1], zi[1:-1,1:-1], cmap=plt.cm.hot)
plt.show()

这篇关于带有数据的 Python 3D 绘图,发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆