简体   繁体   English

matplotlib 奇怪的绘图表面

[英]strange plot surface of matplotlib

I'm trying to generate a 3D height figure, I have a regular grid, the height data collected by the sensor and data store in a file which name is "data.txt".我正在尝试生成一个 3D 高度图,我有一个规则网格,传感器收集的高度数据和数据存储在一个名为“data.txt”的文件中。 data stored one data per line.数据每行存储一个数据。 the file link on github github上的文件链接

import numpy as np
import matplotlib.pyplot as pit

from mpl_toolkits.mplot3d import Axes3D
from scipy.stats import multivariate_normal
from matplotlib import cm

x = np.linspace(0,350,18)
y = np.linspace(0,350,15)
z = np.loadtxt('data.txt')

xx,yy = np.meshgrid(x,y)
fig = pit.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(xx,yy,z)

use the code above, I got a scatter.使用上面的代码,我得到了一个分散。 It looks good!这看起来不错的样子! I found this , I want convert the figure to surface, than I add the code below, but it looks very strange我发现了这个,我想将图形转换为表面,而不是添加下面的代码,但看起来很奇怪

xa = np.reshape(xx, (18,15))
ya = np.reshape(yy, (18,15))
za = np.reshape(z, (18,15))
surf=ax.plot_surface(xa,ya,za,cmap="summer",linewidth=0,antialiased=False, alpha=0.5)
fig.colorbar(surf)
pit.show()

the image图片

i don't know what happened, it look too strange!我不知道发生了什么,看起来太奇怪了! Should i smooth it?我应该平滑它吗?

You need to use xx and yy defined earlier and reshape z to the same shape as xx :您需要使用前面定义的xxyy并将z重塑为与xx相同的形状:

za = z.reshape(xx.shape)
fig = pit.figure()
ax = fig.add_subplot(111,projection='3d')
surf=ax.plot_surface(xx,yy,za,cmap="summer",linewidth=0,antialiased=False, alpha=0.5)
fig.colorbar(surf)
pit.show()

Note that I have rotate the chart for better clarity.请注意,为了更清楚,我已经旋转了图表。

在此处输入图像描述

I think you need scipy.griddata.我认为你需要 scipy.griddata。 Try this code:试试这个代码:

from scipy.interpolate import griddata
za = griddata(x, y, z, (xx, yy), method='linear')
surf=ax.plot_surface(xx,yy,za,cmap="summer",linewidth=0,antialiased=False, alpha=0.5)
fig.colorbar(surf)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM