matplotlib 在不使用轮廓的情况下从 x、y、z 数据集进行 3D 绘图中的颜色

发布于 2024-10-06 20:20:04 字数 2366 浏览 0 评论 0 原文

对于我的一生,我无法弄清楚如何获得与 这个

该链接生成彩色 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

仅显示一种颜色。另请注意,颜色条没有刻度线。

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

For the life of me I cannot figure out how to get the same results as this.

The link generates the colored 3d plot without using contour. If I utilize the same technique but with my own x,y,z data set I get just one color.

The difference must be in the way I'm generating the z data for the plot.

Anyway, using this:

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

Data set:

-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

Only one color is shown. Also notice the color bar has no ticks.

Can you explain what my problem is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两个我 2024-10-13 20:20:04

我认为填充“不连续”表面(网格数据)存在问题。 alt text

代码:

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('300.txt')
x = data[:,0]
y = data[:,1]
z = data[:,2]

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=5, cstride=5, cmap=cm.jet,
                       linewidth=1, antialiased=True)

ax.set_zlim3d(np.min(Z), np.max(Z))
fig.colorbar(surf)

plt.show()

请注意,如果您考虑矩形区域上方的表面(xi x yi),此代码工作正常。换句话说,如果你“剪掉”不规则的边缘。

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

替代文本

I think there is a problem with fill "discontinuous" surface (griddata). alt text

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('300.txt')
x = data[:,0]
y = data[:,1]
z = data[:,2]

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=5, cstride=5, cmap=cm.jet,
                       linewidth=1, antialiased=True)

ax.set_zlim3d(np.min(Z), np.max(Z))
fig.colorbar(surf)

plt.show()

Please note that if you consider the surface above a rectangular area (xi x yi), this code is working properly. In other words, if you "cut off" irregular edges.

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

alt text

随风而去 2024-10-13 20:20:04

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

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

不需要 sys

The easiest way to read text data is via genfromtxt:

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

sys is not required.

半寸时光 2024-10-13 20:20:04

我刚刚遇到了类似的问题。

最后我不得不使用 natgrid (引用 此处但链接不起作用)而不是griddata。

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

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

I just struggled with the similar problem.

Finally I had to use natgrid (which is referenced here but the link doesn't work) instead of griddata.

for me the trick with cutting of plot region didn't work, it was always in one colour.

When installing PyNGL check that you have the latest version of numpy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文